Tag: 的Ruby on 轨道

无法安装gemtherubyracer

在运行bundle install命令时,我遇到以下错误: 使用本机扩展安装libv8(3.3.10.4)Gem :: Installer :: ExtensionBuildError:错误:无法构建gem本机扩展。 D:/RailsInstaller/Ruby1.9.2/bin/ruby.exe extconf.rb * extconf.rb失败*由于某些原因无法创建Makefile,可能缺少必要的库和/或标头。 检查mkmf.log文件以获取更多详细信息。 您可能需要配置选项。 提供的配置选项: – with-opt-dir –without-opt-dir –with-opt-include –without-opt-include = $ {opt-dir} / include –with-opt-lib – without-opt-lib = $ {opt-dir} / lib –with-make-prog –without-make-prog –srcdir =。 –curdir –ruby = D:/RailsInstaller/Ruby1.9.2/bin/ruby检查Python …无法构建libv8:找不到Python! Gem文件将保留在D:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9 .1 / gems / libv8-3.3.10.4中进行检查。 结果记录到D:/ RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/libv8-3.3 .10.4 / ext / libv8 / […]

Ruby on Rails 4.0 – 加载和使用模块

阅读,我对在Rails 4中加载和包含模块的正确方法感到困惑。我想将此模块包含在我的应用程序的几个模型中: module SimpleSearch def self.search(filter) if filter.present? where(‘name like ?’, “%#{filter}%”) else where(‘TRUE’) end end end 文件路径是lib/simple_search.rb感谢Michael的建议,我更新了config/application.rb来帮助加载模块(问题3已经解决): config.autoload_paths += %W(#{config.root}/lib) 在模型中,我包括: class BusinessRule < ActiveRecord::Base extend SimpleSearch 并在执行时获得新错误: undefined method `search’ for # 这里extend相关吗? 我是否使用正确的语法,模块和文件名? 是否有配置以确保加载lib /模块或是否为常规? 谢谢你的帮助, 最好的祝福, 弗雷德

使用Google-Maps-for-Rails Gem如何找到指定距离内指定距离的点?

我想知道是否可以使用Google-Maps-for-Rails gem来查找保存在数据库中的点列表,这些点位于给定点的指定半径范围内。 例如,我如何找到数据库中距离自由女神像20英里范围内的所有点。 除非我错过了,否则我在文档中找不到任何内容。 谢谢你的期待。

如何在表中搜索类似记录并显示计数 – Ruby on Rails

我在我正在构建的Ruby on Rails应用程序中创建了一个名为Tags的表。 它是一个博客应用程序,所以我允许用户将标签与post关联,并通过:posts,:has_many =>标签和标签belongs_to:post association进行。 现在我有了我的标签表,我试图看看如何渲染视图,使其显示标签和标签计数。 (应该注意的是,我试图在/views/posts/index.html.erb文件中渲染它)。 例如,如果Tag表中有10个条目用于tag_name Sports。 如何在视图中显示运动(10)。 我不打算为特定标签执行此操作,而是以某种方式搜索表格,组合标签并显示所有标签的列表以及旁边的计数。 (我真的希望这些链接指向包含该标签的post列表,但我很早就学会了一次只问一个问题)。 希望有道理。 更新评论 视图 tag_name)) %> () postsController: def index @tag_counts = Tag.count(:group => :tag_name) @posts = Post.all :order => “created_at DESC” respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } format.json { render :json => @posts } format.atom […]

使用Rails创建动作API。 我需要使用respond_with吗?

我试图找出可以在Rails API中创建创建操作的不同方法。 这是我的索引操作(有效)和我当前的创建操作实现。 routes.rb文件: Rails.application.routes.draw do namespace :api do namespace :v1 do resources :vendors end end end 控制器: class Api::V1::SuyasController < ApplicationController def index render json: Suya.all end def create render json: Suya.create(suyas_params) end private def suyas_params require(:suya).permit(:meat, :spicy) end end 我需要使用respond_with / respond_to吗? 这被抽象出来的responders.gem。 如果我不想使用响应者gem这是创建api的最佳方法吗?

默认范围内的参数数量(0表示1)错误

我一直收到错误ArgumentError: wrong number of arguments (0 for 1)我的default_scope ArgumentError: wrong number of arguments (0 for 1) ,这是default_scope { where(“#{table_name}.tenant_id IS NULL”) } 它一直给我这个错误,我不明白为什么。 我的用户模型中有默认范围。 更新: 使用rails console时输出错误: ArgumentError: wrong number of arguments (0 for 1) from /home/evan/Apps/demo-application/app/models/user.rb:18:in `hash’ from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping.rb:64:in `value_for’ from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `public_send’ from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `block in method_missing’ from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:123:in `ignore_default_scope?’ from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:134:in `evaluate_default_scope’ […]

用户未登录时如何将用户重定向到登录页面

当用户登录时,我们将其用户ID存储在会话中。 session[:user_id] = user.id 现在在我们网站的所有其他链接上,如果session[:user_id] == nil ,我们希望用户重定向 我认为它的方式是它将在控制器的每个方法中完成。 def show_customers if session[:user_id] == nil redirect_to (:controller => “authentication”, :action => “login”) #code related to show_customers goes here end 但这需要在每个控制器的每个方法中完成。 是否有更理智的Rails’y方式来做到这一点?

link_to机制如何在Ruby on Rails中运行

我根据给出的指示实现了官方创建博客应用程序项目。 但我不知道在这个项目中使用的link_to的想法如下: ‘Are you sure?’, :method => :delete %> 在app/views/posts/index.html.erb文件中给出, app/controllers/posts_controller.rb相应代码用于在app/views/posts/目录中呈现html页面。 如果我想渲染一个新的html页面,请说app/views/posts/目录中的index2.html.erb与index.html.erb相比没有’Edit’和’Destroy’链接,那么我应该如何编写link_to和posts_controller.rb相应代码?

Rails模式accepted_nested_attributes_for正常工作

我有这样的模型: class User < ActiveRecord::Base has_one :business end class Business lambda { |a| a[:location].blank?} atr_accessible :name, :locations_attributes … end class Location < ActiveRecord::Base belongs_to :business … end 当我在表单中填写地址,并将表单发布到BusinessesController的create操作时,日志显示参数是正确的: … “business”=>{“name”=>”sos”,”locations_attributes”=>{“0″=>{“address”=>”location1”}, “1”=>{“address”=>”location2”}}} … 在BusinessesController的create操作中 # :Post /usres/1/businesses def create @user = User.find(params[:user_id]) @business = @user.build_business(params[:business]) if @business.save … else … end end 我检查日志,发现@business.save没有向数据库插入任何有关locaitons的信息,但只有关于业务的信息,但是params[:business]显然包含了hash的位置,所以我错了?

Ruby on Rails,通过购买订购前5名用户

我有2个型号,用户和购买。 User has_many :purchases和Purchase belongs_to :user 购买领域是: id, product_id, user_id, amount, created_at 我想要实现的是一个方法调用,例如: User.top_five ,它将返回具有最高购买价值的5个用户,即每个用户的purchases.amount字段的总和。 我也希望能够做类似User.top_five(:start_date=>’01/01/2010′,:end_date=>’31/12/2010′)事情,即选择一个时间段来计算前五个用户。 所以我一直在努力获得连接,总和,order_bys等的正确组合,但我只是没有得到它。 所以希望有人可以指出我正确的方向! 希望我在这里给出足够的信息,这是我的第一个问题。 谢谢