简体   繁体   中英

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.

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

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?

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:

<%= 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM