Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin Redirect After Create?

In activeadmin, I would like to know how to redirect to an url with a parameter after a record is created?

So far I have the following code which works except 'event_id' is empty.

Eg:

http://0.0.0.0:3000/admin/events/new?event_id=369877

Code

  controller do
    def create
      create! do |format|
        parameters = Rack::Utils.parse_query URI(request.referrer).query
        format.html { redirect_to '/admin/events?q[espn_event_id_equals]='+ parameters['espn_event_id'].to_s }
      end
    end
  end

It rediects to:

http://0.0.0.0:3000/admin/events?q[event_id_equals]=

instead of

http://0.0.0.0:3000/admin/events?q[event_id_equals]=369877

Thanks in advance

SOLUTION

The solution was to parse the URL from request.referrer to get the desired parameter.

  controller do
    def create
      create! do |format|
        parameters = Rack::Utils.parse_query URI(request.referrer).query
        format.html { redirect_to '/admin/events?q[espn_event_id_equals]='+ parameters['espn_event_id'].to_s }
      end
    end
  end

Thanks for the suggestions : )

like image 549
ipegasus Avatar asked Oct 23 '25 15:10

ipegasus


2 Answers

First, try keying the param to a symbol, rather than a string:

format.html { redirect_to '/admin/events?q[event_id_equals]='+ params[:event_id].to_s }

If that doesn't work, it means (more likely than not) that params[:event_id] is nil. Because nil.to_s is an empty string, you're not seeing anything being appended to the specified path.

like image 148
zeantsoi Avatar answered Oct 25 '25 05:10

zeantsoi


Well, you structure is a bit confusing. But I think this is pretty straight forward:

format.html { redirect_to admin_events_path(q[event_id_equals]: params[:event_id]) }

if this doesn't work, that's probably because it can't read q[event_id_equals] as a symbol, in this case you shoudl try: admin_events_path("q[event_id_equals]" => params[:event_id])

There are probably better ways of naming and controlling this, but this will certainly fix things for you.

For active_admin 1.0.0.pre2:

format.html { redirect_to admin_events_path('q[event_id_eq]': params[:event_id]) }
like image 23
ScieCode Avatar answered Oct 25 '25 03:10

ScieCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!