rails-搜索表单的多个路径

时间:2018-08-30 09:31:10

标签: ruby-on-rails

我正在实施网站。搜索表单时出现问题。我上传了代码,我想问的是如何通过homes_controller上的“ index”和“ historical”设置搜索“ path” 下面是我的代码:

app / controllers / homes_controller

 def index
    @homes = Home.where(:category => 1).reverse
 end

 def historical
    @homes = Home.where(:category => 2).reverse
 end

app / views / layouts / application.html.erb

下面,此代码现在是临时代码。我应该改变它。

<%= form_tag(homes_path, :method => 'get', id: "search-form" do %>
    <%= text_field_tag :search, params[:search], placeholder: "검색" %>
    <%= submit_tag "검색", :name => nil %>
  <% end %>

1 个答案:

答案 0 :(得分:0)

不确定在这里应该做什么

但是根据问题-我可以为您的问题提供解决方案

在控制器操作中保留实例变量-像这样

app / controllers / homes_controller

 def index
    @homes = Home.where(:category => 1).reverse
    @search_path = "path you want to give" 
 end

 def historical
    @homes = Home.where(:category => 2).reverse
    @search_path = "path you want to give" 
 end

在您的布局中,您可以像这样使用它

app / views / layouts / application.html.erb

<%= @search_path.present? %>

  <%= form_tag(@search_path, :method => 'get', id: "search-form" do %>
    <%= text_field_tag :search, params[:search], placeholder: "검색" %>
    <%= submit_tag "검색", :name => nil %>
  <% end %>
<% end %>