0

我正在处理我的第一个 Rails 项目,但无法让我的查询正常工作。我的总体目标是设置一个范围集合来创建一些具有多个选项的长查询。但是,首先,我只是想设置一个范围,从单个模型中查询单个列。这是我的模型:

 class Drawing < ActiveRecord::Base
  has_many :revisions
  scope :by_description, lambda { |description| where(description: description) unless         description.nil? }
 end

这是我的控制器的索引操作:

def index
    @drawings = Drawing.by_description(params[:description]).all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @drawings }
    end
  end

该视图是 rails 的默认索引视图。

当我在搜索字段中输入输入(例如“案例”)时,提交按钮会起作用并将我引导至索引视图,但会列出所有“图纸”,而不仅仅是将“案例”作为其描述。非常感谢你的帮助。

这是适用的开发日志文件部分,如您所见,生成的 SQL 不包括添加的描述参数:

Started GET "/drawings?utf8=%E2%9C%93&drawings%5Bdescription%5D=Case&drawings%5Bdrawing_number%5D=&drawings%5Bitem_number%5D=&drawings%5Bpump_model%5D=&drawings%5Bframe_size%5D=&drawings%5Bpart_type%5D=&drawings%5Bcreated_before%281i%29%5D=&drawings%5Bcreated_before%282i%29%5D=&drawings%5Bcreated_before%283i%29%5D=&drawings%5Bcreated_after%281i%29%5D=&drawings%5Bcreated_after%282i%29%5D=&drawings%5Bcreated_after%283i%29%5D=&commit=Search" for 127.6.43.1 at 2013-08-31 11:39:28 -0400
Processing by DrawingsController#index as HTML
  Parameters: {"utf8"=>"✓", "drawings"=>{"description"=>"Case", "drawing_number"=>"", "item_number"=>"", "pump_model"=>"", "frame_size"=>"", "part_type"=>"", "created_before(1i)"=>"", "created_before(2i)"=>"", "created_before(3i)"=>"", "created_after(1i)"=>"", "created_after(2i)"=>"", "created_after(3i)"=>""}, "commit"=>"Search"}
  [1m[36mDrawing Load (0.3ms)[0m  [1mSELECT "drawings".* FROM "drawings" [0m
  Rendered drawings/index.html.erb within layouts/application (6.0ms)
Completed 200 OK in 226ms (Views: 186.8ms | ActiveRecord: 0.3ms)
4

1 回答 1

3

参数是错误的。

代替

params[:description]

利用

params[:drawings][:description]
于 2013-08-31T16:24:10.643 回答