如何使用Rails选择与搜索

时间:2014-02-06 01:07:36

标签: ruby-on-rails ruby-on-rails-3.2

我需要使用Rails 选择方法来反对* text_field *来搜索旧数据。不幸的是,Project表中的一个属性是大写的,即使我在其上使用alias_attribute,它仍然会抱怨从实际表中正确命名。代码打击工作到一定程度,但不是将Project_cont作为参数中的名称值传递,而是获得该描述的id。我怎样才能做到这一点?这是我目前的代码:

 = search_form_for @search, :html => {:class => "form-inline"} do |f|
      .form-group
        = f.select :Decsription_cont, options_from_collection_for_select(Project.all, "id","Description", @search)

这是控制器的代码:    

    def index
      @search = Project.search(params[:q])
      @products = @search.result
    end
   
这是视图的代码:

   = search_form_for @search, :html => {:class => "form-inline"} do |f|
     = f.select :Decsription_cont, options_from_collection_for_select(Project.all, "id","Description", @search.description_cont)
     = f.submit
  

1 个答案:

答案 0 :(得分:0)

您应该description_cont添加@search,如下所示:

f.select :description_cont, options_from_collection_for_select(Project.all, "id", "description", @search.description_cont)