我有一个简单的rails搜索表单,它将在一个列中选择3个不同排名的列。但我不能扩展多个等级。
表食物:value1,rank_value1,value2,rank_value2,value3,rank_value3
查看/ food / index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search)
if search
Food.where(["rank_value1= ?", "#{search}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
@foods = Food.search(params[:rvalue1])
end
为了扩展两个等级,我尝试了以下代码,但它没有用。
查看/ food / index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= text_field_tag :rvalue2, params[:rvalue2] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search1,search2)
if search
Food.where(["rank_value1= ? and rank_value2=?", "#{search1}", "# {search2}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
@foods = Food.search(params[:rvalue1], params[:rvalue2])
end
有关在多列中搜索数据的任何建议吗?
答案 0 :(得分:0)
嘿,你可以使用Ransack gem来搜索多个值。在这里,您可以看到视频教程和github链接以供实施。
github:https://github.com/activerecord-hackery/ransack
视频:http://railscasts.com/episodes/370-ransack
希望你只是在寻找这个。