select_tag选择哪个作为PARAM真正传递到查询字符串

问题描述:

我有以下代码:select_tag选择哪个作为PARAM真正传递到查询字符串

<%= form_tag users_path, method: :get do %> 
    <%= check_box_tag 'popularity', true %> 
    <%= submit_tag "Search", :name = "nil", :id => "submit" %> 
    <% end % 

时选择它导致:

www.example.com/users?popularity =真

但是,因为我有多个订单范围,我想要做的是创造一个select_tag用下拉列表中,凡使用可以popularity之间进行选择,photos_countrecent_activity和 他选择将传递到查询字符串这样:

example.com/users?popularity=true

例子。 COM /用户?PHOTOS_COUNT =真

example.com/users?recent_activity=true

+0

那么如果你要*选择选项之间*,轨道有一个标签也没有了吗? – MageeWorld

+0

你能更具体吗?我不知道如何使用'options_for_select'上述目标 –

+0

http://guides.rubyonrails.org/form_helpers.html – MageeWorld

没有实际上是:在我的用户数据库SORT_TYPE列。我只是用enum作为组织范围,以使它们中的一个可以传递到查询字符串的方式。

<%= form_tag users_path, method: :get do %>  
    <%= select_tag :sort_type, options_for_select(User.sort_types, :selected => params[:sort_type]), :include_blank => true %> 
<% end %> 

class User 
    enum sort_type: ["popularity", "number of photos"] 
end 

我然后使用控制器来设置基于存在params范围[:SORT_TYPE]整数,通入从表单查询字符串。