[英]keep meta_search parameters for edit page?
I'm using meta_search gem.I have url like this for admin projects index page with search parameters. 我正在使用meta_search gem。我有类似url的带有搜索参数的管理项目索引页面。
admin/projects?utf8=✓&search%5Bid_equals%5D=&search%5Btitle_contains%5D=&search%5Bstage_in%5D=completed
Then user choose one project and url will be this 然后用户选择一个项目,URL将是这个
admin/projects/a--15/edit?page=1
When user update this form,The search parameters will be lost. 当用户更新此表格时,搜索参数将丢失。
How can i keep these parameters.I mean with session or meta_search have some method to fix this? 我该如何保留这些参数。我的意思是使用session或meta_search可以解决此问题?
First, create a filter that fires for every action that might want access to the search parameters: 首先,创建一个过滤器,该过滤器针对可能想要访问搜索参数的每个操作触发:
ProjectsController < ApplicationController
before_filter :save_searches
def save_searches
@addons = ''
[:page, :id_equals,:title_contains,:stage_in].each do |k|
if params[k]
pval = params[k].is_a?(Array) ? params[k].join(',') : params[k]
@addons << k.to_s + "=" + pval + "&"
end
end
@addons.chop!
end
Now, when your actions fire, @addons will be set, and then you can do this: 现在,当您的操作触发时,将设置@addons,然后您可以执行以下操作:
<%= link_to 'Edit' , edit_path(@project.id) + @addons.length > 0 ? "?" + @addons : '' %>
That said, I'm willing to bet this is somewhat of a hack, and there is a cleaner way to do it. 就是说,我愿意打赌这有点像黑客,而且还有一种更清洁的方式来做到这一点。 But this works for me.
但这对我有用。
The :page key makes it so that if you have pagination running and using the :page param to track current page, your pagination should be remembered as well. 使用:page键可以使您运行分页并使用:page参数跟踪当前页面,也应记住您的分页。
Also note that in the event of you getting an Array in your params, (ie the result of a select with :multiple=>true), this is handled. 还要注意,如果您在参数中获得了Array(即使用:multiple => true进行选择的结果),则将对此进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.