rails形成f.select错误

时间:2012-12-23 14:39:36

标签: ruby-on-rails forms

我想使用meta_search gem。在我看来,我有这个:

<%= f.select :project_type_id_in, 
   raw("<option value=''>-- Proje Types --</option>" + 
   options_from_collection_for_select(ProjectType.all, :id, :name, 
   params[:search][:project_type_id_in].to_i )) %>

但是当我打电话给控制器时我得到了这个错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

1 个答案:

答案 0 :(得分:0)

params[:search]似乎是nil,可能是因为尚未指定搜索选项。您可以确保在页面加载之前初始化它以避免这种情况。或者你可以像这样解决它:

<%= f.select :project_type_id_in, 
      raw("<option value=''>-- Proje Types --</option>" + 
      options_from_collection_for_select(ProjectType.all, :id, :name, 
      params[:search].present? ? params[:search][:project_type_id_in].to_i : nil)) %>

或者您可以使用andand gem来做同样的事情:

<%= f.select :project_type_id_in, 
      raw("<option value=''>-- Proje Types --</option>" + 
      options_from_collection_for_select(ProjectType.all, :id, :name, 
      params[:search]&&.[:project_type_id_in].to_i)) %>