我现在有一个简单的搜索表单,它将搜索模型中的一列并按预期返回结果,但是我希望通过能够使用4 text_field / Select_tags搜索同一模型中的4列来构建此基础。
我已经看过Gems,它提供了类似于solr的例子,但我的想法是,对于我想要实现的目标,它似乎有点过分,特别是在如此小的应用程序上
应用程序很简单,您可以上传食谱,将食谱添加到收藏夹和搜索食谱。
到目前为止它看起来像这样
控制器
def search
@countrysearch = Recipe.where(:country_of_origin => params[:search]).all
end
搜索表单
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>
输出(查看)
<% @countrysearch.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
<td><%= r.country_of_origin %></td>
<td><%= r.difficulty %></td>
<td><%= r.preperation_time %></td>
<td><%= ingredient_names(r.ingredients) %></td>
<td><%= preperation_steps(r.preperations) %></td>
</tr>
我希望我的搜索表单能够使用选择标记来反映我的“创建新配方表单”
<%= f.label :dish_name, "Dish Name" %>
<%= f.text_field :dish_name, :placeholder => "Enter Dish Name" %>
<%= f.label :country_of_origin, "Country Of Origin" %>
<%= f.select :country_of_origin, [['Wales'],['Scotland'],['England']], {:include_blank => 'Please Select'} %>
<%= f.label :difficulty, "Difficulty Level" %>
<%= f.select :difficulty, [['Beginner'],['Intermediate'],['Expert']], {:include_blank => 'Please Select'} %>
<%= f.select :preperation_time, [['15..30'],['30..60'],['60..120']], {:include_blank => 'Please Select'} %>
<% end %>
只是在正确的方向上的一些指示将不胜感激。谢谢
答案 0 :(得分:2)
您可以使用传递给控制器的所有参数来执行此操作:
def search
@countrysearch = Recipe.where({:dish_name => params[:search][:dish_name], :country_of_origin => params[:search][:country_of_origin], :difficulty => params[:search][:difficulty], :preperation_time => params[:search][:preperation_time]}).all
end
这对你有用。还有其他选择,但如果你想要一个简单的选择,这可能适合你。
修改强>
如果您在搜索中没有其他内容,可以执行以下操作:
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :dish_name, params[:dish_name] %>
<%= select_tag "country_of_origin", options_from_collection_for_select(@recipes, "country_of_origin", "country_of_origin_name")
<%= select_tag "difficulty ", options_from_collection_for_select(@recipe, "difficulty ", "difficulty ")
<%= select_tag "preperation_time", options_from_collection_for_select(@recipe, "preperation_time", "preperation_time")
<%= submit_tag "Search" %>
<% end %>
然后在控制器中直接传递参数
def search
@countrysearch = Recipe.where(params).all
end