2016-03-27 21 views
0

私はcollection_selectドロップダウンボックスに基づいていくつかのデータをフィルタリングしようとしています。rails collection_select params

私は正常にtext_field_tagを使用してデータをフィルタリングすることができます。したがって、フィルタは正常に動作していると仮定しますが、同じ処理を行うcollection_selectを取得できません。

text_field_tagに1を入力すると、パラメータの一部として「検索」=>「1」が生成されますが、私がcollection_selectから選択すると... {"utf8" => " ✓」、 "" => { "検索" => "1"}、...

index.html.erb

<h1>Students#index</h1> 
<p>Find me in app/views/students/index.html.erb</p> 

<%= form_tag students_path, :method => 'get' do %> 
    <%= collection_select :search , :search.to_s, Tutor.all, :id, :name, prompt: true %> 
    <%= submit_tag "search" %> 
<% end %> 

<% @students.each do |n| %> 
    <li> 
    <%= link_to n.first_name, student_path(n) %> 
    <%= n.surname %> ..tutor is... 
    <%= n.tutor.name %> 
    </li> 
<% end %> 

<%= params.inspect %> 

<%= form_tag(students_path, :method=> "get", id: "search-form") do %> 
    <%= text_field_tag :search, params[:search], placeholder: "Search Students" %> 
    <%= submit_tag "Search", :name => nil %> 
<% end %> 

student.rb

class Student < ActiveRecord::Base 
    belongs_to :tutor 

    def self.search(search) 
    where("tutor_id LIKE ?","%#{search }%") 
    end 
end 

students_controller検索。 rb

class StudentsController < ApplicationController 
    def index 
    if params[:search] 
     @students = Student.search(params[:search]) 
    else 
     @students = Student.all 
    end 
    end 

答えて

0

つまり、collection_selectが機能します。あなたのparamsはそのように見えるようcollection_selectの最初のパラメータは、ない方法オブジェクトです。

params[:search]からparams[:search][:search]に変更すると問題が解決するはずです。

class StudentsController < ApplicationController 
    def index 
    if params[:search][:search] 
     @students = Student.search(params[:search][:search]) 
    else 
     @students = Student.all 
    end 
    end 
end 
+0

ありがとうPavan、それは働いた:-) – futureprogress

関連する問題