通过一个搜索框和两个下拉框过滤索引页面
Filtering index page by one search box and two dropdown boxes
我正在尝试根据搜索框的内容以及在其他 2 个下拉列表中选择的任何内容来过滤索引页的结果。
我正在使用 simple_form,我已经让搜索框工作并使用以下参数之一过滤结果:
我的看法:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= submit_tag 'Search' , name: nil %>
<% end %>
我的索引方法:
def index
@entitys = if params[:name_filter]
Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#
{params[:name_filter]}%".downcase).paginate(page:
params[:page], per_page: 8)
else
@entitys = Entity.all.paginate(page: params[:page],
per_page: 8)
end
end
我不知道如何再制作 2 个下拉菜单并将它们应用于索引方法过滤。
提前致谢。
从评论来看,问题似乎是关于如何填充 select 标签。这是需要的方法
所以它们可能看起来像这样:
<%= select_tag(
'location',
options_from_collection_for_select(
get_all_locations,
'name', # The method called to generate the value passed up to the controller
'name' # The value displayed to users
)
) %>
这将在提交表单时填充 params[:location]
。
查看:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= select_tag :location_filter, options_for_select(get_city_list) %>
<%= select_tag :activity_filter, options_for_select(get_professional_activity_list) %>
<%= submit_tag 'Procurar' , name: nil %>
<% end %>
控制器索引方法:
def index
if params[:name_filter].present? || params[:activity_filter].present? || params[:location_filter].present?
@entitys = Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#{params[:name_filter]}%".downcase).where('LOWER(entities.location) LIKE?', "%#{params[:location_filter]}%".downcase).where('LOWER(entities.professional_activity) LIKE?', "%#{params[:activity_filter]}%".downcase).paginate(page: params[:page], per_page: 8)
else
@entitys = Entity.all.paginate(page: params[:page], per_page: 8)
end
end
enter code here
这满足了我的需要(或者看起来如此)
我正在尝试根据搜索框的内容以及在其他 2 个下拉列表中选择的任何内容来过滤索引页的结果。
我正在使用 simple_form,我已经让搜索框工作并使用以下参数之一过滤结果:
我的看法:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= submit_tag 'Search' , name: nil %>
<% end %>
我的索引方法:
def index
@entitys = if params[:name_filter]
Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#
{params[:name_filter]}%".downcase).paginate(page:
params[:page], per_page: 8)
else
@entitys = Entity.all.paginate(page: params[:page],
per_page: 8)
end
end
我不知道如何再制作 2 个下拉菜单并将它们应用于索引方法过滤。 提前致谢。
从评论来看,问题似乎是关于如何填充 select 标签。这是需要的方法
所以它们可能看起来像这样:
<%= select_tag(
'location',
options_from_collection_for_select(
get_all_locations,
'name', # The method called to generate the value passed up to the controller
'name' # The value displayed to users
)
) %>
这将在提交表单时填充 params[:location]
。
查看:
<%= form_tag(entitys_path, method: :get) do %>
<%= text_field_tag :name_filter, params[:name_filter] %>
<%= select_tag :location_filter, options_for_select(get_city_list) %>
<%= select_tag :activity_filter, options_for_select(get_professional_activity_list) %>
<%= submit_tag 'Procurar' , name: nil %>
<% end %>
控制器索引方法:
def index
if params[:name_filter].present? || params[:activity_filter].present? || params[:location_filter].present?
@entitys = Entity.joins(:user).where('LOWER(users.name) LIKE?', "%#{params[:name_filter]}%".downcase).where('LOWER(entities.location) LIKE?', "%#{params[:location_filter]}%".downcase).where('LOWER(entities.professional_activity) LIKE?', "%#{params[:activity_filter]}%".downcase).paginate(page: params[:page], per_page: 8)
else
@entitys = Entity.all.paginate(page: params[:page], per_page: 8)
end
end
enter code here
这满足了我的需要(或者看起来如此)