我是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>
我想要实现的是,根据用户在下拉列表中选择的内容,将用户定向到该特定页面。
示例:如果用户在搜索时选择了产品,我希望将用户定向到产品页面。如果用户在搜索时选择了供应商,则将用户定向到供应商页面,依此类推。 form_tag接受单个路径,它适用于该视图,但由于搜索栏涉及多个视图,我如何根据用户的下拉选择传递路径?任何帮助将不胜感激!
答案 0 :(得分:1)
我认为您只需检查一些可以发送的参数,具体取决于用户选择,这样您就不必添加多路径,但是,请在控制器中检查参数并根据此值重定向。
可能是这样的:
<%= form_tag search_path, method: 'get', id: 'search-form' do %>
...
如果用户选择了一些您可以识别为供应商的值,那么您将重定向到供应商,产品等:
def search
# vendor redirection
if params[:dropdown_selections] == ...
redirect_to vendors_path
else
redirect_to another_path
...
根据你的回答,我找到了一个可能的解决方案。
表单指向创建的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 %>
在select选项中,用户可以选择所有可能的值,这将是创建在控制器中重定向的路径的方法:
def search
options = ['products', 'vendors', 'manufacturers']
selection = params[:dropdown_selection]
known_path = options.any?(&selection.method(:==))
route_to_redirect = known_path ? selection.to_sym : root_path
redirect_to route_to_redirect
end
options
与select标记中使用的内容相同。selection
存储用户选择的值。known_path
检查选项中的值是否为选项数组中的任何值。route
检查known_path变量是否为true,如果是,则将选定的值转换为其表示形式,以将其用作路径。如果不是,则重定向到root_path。redirect_to
使用创建的路由进行重定向。答案 1 :(得分:1)
谢谢Sebastiàn,我是rails的新手,我一直坚持这个问题所以我决定用jquery来解决它。为了帮助将来的人,这是我使用的步骤。
步骤1:从我的路线文件的下拉列表中定义我想要重定向的所有路径。 第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_for(:controller =&gt; controller_name,:action =&gt; action_name)
<%= 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 %>
步骤2:创建搜索控制器
步骤3:创建处理所有搜索逻辑的操作
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
步骤4:为搜索控制器创建路线
get '/search', to: 'search#primary_search'