简体   繁体   English

rails-搜索表单的多个路径

[英]rails - multiple paths for a search form

I'm implementing the website. 我正在实施网站。 I got a problem for a search form. 搜索表单时出现问题。 I upload my code, and what i want to ask is how to set the search 'path' through 'index' and 'historical' on homes_controller Below, my code: 我上传了我的代码,我想问的是如何通过homes_controller上的“ index”和“ historical”设置搜索“ path”,下面是我的代码:

app/controllers/homes_controller 应用程序/控制器/ 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 应用程序/视图/布局/ application.html.erb

Below, this code is temporary code for now. 在下面,此代码现在是临时代码。 I should change it. 我应该改变它。

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

Am not sure what you are supposed to do here 不知道你应该在这里做什么

But as per the question - I can give a solution to your problem 但是根据问题-我可以为您的问题提供解决方案

Keep an instance variable in your controller actions - like this 在控制器动作中保留实例变量-像这样

app/controllers/homes_controller 应用程序/控制器/ 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

and in your layout you can use it like this 在您的布局中,您可以像这样使用它

app/views/layouts/application.html.erb 应用程序/视图/布局/ 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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM