Ruby Rails 5-如何为搜索栏设置多个路径

Ruby Rails 5-如何为搜索栏设置多个路径,ruby,ruby-on-rails-5,Ruby,Ruby On Rails 5,我是rails新手,尝试创建一个带有简单文本字段、下拉选择和搜索按钮的搜索栏。这是视图代码: <div> <%= form_tag [products_path,vendors_path...], :method => 'get', id: "search-form" do %> <div class="ui action input" id="home-page-search-textbox-container"> <!-

我是rails新手,尝试创建一个带有简单文本字段、下拉选择和搜索按钮的搜索栏。这是视图代码:

<div>
  <%= form_tag [products_path,vendors_path...], :method => 'get', id: "search-form" do %>
    <div class="ui action input" id="home-page-search-textbox-container">
      <!-- SEARCH TEXTBOX-->
      <%= text_field_tag(:search, params[:search],
          placeholder: 'Products, Manufacturers, Vendors...')
      %>
      <!-- SEARCH DROPDOWN SELECTION -->
      <%= select_tag :dropdown_selection,
          options_for_select(@dropdown_selections),
          {:class => "ui selection dropdown",}
      %>
      <!-- SEARCH BUTTON -->
      <div>
        <%= button_tag("Search", :class => "ui button", :name => nil) %>
      </div>
    </div>
  <% end %>
</div>

“获取”,id:“搜索表单”do%>
“用户界面选择下拉列表”,}
%>
“ui按钮”,:name=>nil)%>
我试图实现的是,根据用户在下拉列表中选择的内容,将用户引导到该特定页面


示例:如果用户在搜索时选择了产品,我希望将用户定向到产品页面。如果用户在搜索时选择了供应商,则将用户指向供应商页面,依此类推。form_标记接受一条路径,它将适用于该视图,但由于搜索栏涉及多个视图,如何根据用户的下拉选择传递路径?任何帮助都将不胜感激

我认为您可以根据用户选择检查一些可以发送的参数,这样您就不必添加多个路由,但是,在控制器中检查参数并根据此值重定向

可能是这样的:

<%= form_tag search_path, method: 'get', id: 'search-form' do %>
  ...

根据你的回答,我找到了一个可能的解决办法

表单指向创建的GET路径,这是

get 'search', to: 'home#search'
加上所有其他要重定向的路径

<%= form_tag 'search', method: :get do %>
  <%= select_tag :dropdown_selection, options_for_select(['products', 'vendors', 'manufacturers', 'others']) %>
  <%= submit_tag 'Send' %>
<% end %>
  • 选项与选择标记中使用的选项相同
  • 选择
    存储用户选择的值
  • known_path
    检查所选值是否在选项数组中
  • route
    检查已知路径变量是否为true,如果为true,则将所选值转换为其表示形式作为符号,以将其用作路由。如果不是,则重定向到根路径
  • redirect\u to
    使用创建的路由进行重定向

    • 谢谢Sebastián,我是rails的新手,在这个问题上我被困了一段时间,所以我决定用jquery解决它。为了在将来帮助某人,以下是我使用的步骤

      步骤1:从routes文件的下拉列表中定义要重定向的所有路径。 步骤2:通过jquery实现功能

      var searched_text;
        var main_search_selections;
      
        // 1 - get selection values on change first
        $('select').change(function() {
          main_search_selections = $(this).val().toLowerCase();
        });
      
        // 2 - take user to a page based on their selection
        $('#home-page-search-button').click(function(){
          // if the dropdown selection is vendors when searching
          if(main_search_selections == "vendors") {
            // go to vendors result page
            window.location.href='/result/vendors';
          }
          // if the dropdown selection is products when searching
          else if (main_search_selections == "products") {
            // go to products result page
            window.location.href='/result/products';
          }
          // if the dropdown selection is manufacturers when searching
          else if (main_search_selections == "manufacturers") {
            // go to the manufacturers result page
            window.location.href='/result/manufacturers';
          }
          // else if the only selection left would be all
          else {
            // go to the all result page
            window.location.href='/result/all';
          }
        });
      
      编辑: 上述解决方案仅适用于将用户重定向到正确的页面。我无法使用params[:search]来执行搜索逻辑。使用@Sebastiá的答案,我能够解决我的问题

      步骤1:更新搜索表单:已使用的url\u(:controller=>controller\u name,:action=>action\u name)

      步骤4:为搜索控制器创建路由

      get   '/search', to: 'search#primary_search'
      

      在下拉列表中,如何区分产品和供应商等?我可以访问用户在运行时使用params对象选择的值(params[:dropdown_selections])。此外,产品和供应商也是模型。不确定这是否是您要问的问题。我的意思是,如果所选值来自下拉列表,并且您希望重定向到不同的路径,则需要检查哪个路径对应于所选值。我已使用可能的Rails解决方案更新了答案。我将尝试使用您的建议,因为它是纯Rails解决方案,我将试着看看我是否能让它工作,并在确认后将其标记为答案,因为它不涉及任何js代码。谢谢你的帮助!嘿@Sebastián Palma,我试图按照你的建议实施,但没有任何成功。这是我的路由文件:get'/result/all',到:'static_pages#all_result_page'get'/result/products',到:'products#index'get'/result/manufacturers',到:'manufacturers#index'get'/result/vendors',to:'vendors#index'添加了搜索功能,但页面没有正确重定向。我的问题只对表单做了一点小小的更改,您的答案就准确无误了。我使用的表单标签只有一个路径('get',id:“search form”do%>)。相反,我需要使用url\u for方法来指定表单要使用的控制器和操作,并在该控制器中('search',:action=>'primary\u search'),:method=>'get',id:“search form”do%>)。之后,我可以在搜索控制器的主搜索操作中根据用户的下拉选择呈现页面。这很好,我尝试使用与路由中相同的名称,以便将其用作创建路由的符号。很高兴它起作用了。
      var searched_text;
        var main_search_selections;
      
        // 1 - get selection values on change first
        $('select').change(function() {
          main_search_selections = $(this).val().toLowerCase();
        });
      
        // 2 - take user to a page based on their selection
        $('#home-page-search-button').click(function(){
          // if the dropdown selection is vendors when searching
          if(main_search_selections == "vendors") {
            // go to vendors result page
            window.location.href='/result/vendors';
          }
          // if the dropdown selection is products when searching
          else if (main_search_selections == "products") {
            // go to products result page
            window.location.href='/result/products';
          }
          // if the dropdown selection is manufacturers when searching
          else if (main_search_selections == "manufacturers") {
            // go to the manufacturers result page
            window.location.href='/result/manufacturers';
          }
          // else if the only selection left would be all
          else {
            // go to the all result page
            window.location.href='/result/all';
          }
        });
      
      <%= form_tag url_for(:controller => 'search', :action => 'primary_search'), :method => 'get', id: "search-form" do %>
              <div class="ui action input" id="home-page-search-textbox-container">
                <!-- SEARCH TEXTBOX-->
                <%= text_field_tag(:search, params[:search],
                    placeholder: 'Products, Manufacturers, Vendors...')
                %>
                <!-- SEARCH DROPDOWN SELECTION -->
                <%= select_tag :dropdown_selection,
                    options_for_select(@dropdown_selections),
                    {:class => "ui selection dropdown",}
                %>
                <!-- SEARCH BUTTON -->
                <div id="home-page-search-button">
                  <%= button_tag("Search", :class => "ui button", :name => nil) %>
                </div>
              </div>
            <% end %>
      
          def primary_search
              @dropdown_selections   = ['All', 'Products', 'Manufacturers', 'Vendors']
              @product_categories    = Product.all.map { |p| p.category }.sort.uniq
              @product_manufacturers = Manufacturer.all.map {|m| m.name }.uniq
              @product_vendors       = Vendor.all.map {|v| v.name }.uniq
      
              if params[:search].present?
                if params[:dropdown_selection].to_s.downcase == 'all'
                  @products = Product.search_all(params[:search])
                  render "/static_pages/all_result_page"
                elsif params[:dropdown_selection].to_s.downcase == 'products'
                  @products = Product.search_products(params[:search])
                  render "products_result_page"
                elsif params[:dropdown_selection].to_s.downcase == 'manufacturers'
                  @products = Product.search_manufacturers(params[:search])
                  render "/manufacturers/manufacturers_result_page"
                elsif params[:dropdown_selection].to_s.downcase == 'vendors'
                  @products = Product.search_vendors(params[:search])
                  render "/vendors/vendors_result_page"
                end
              else
                @products = Product.all
                render "/static_pages/all_result_page"
              end
            end
      end
      
      get   '/search', to: 'search#primary_search'