Question

I have a trackers_controler with a destroy action. I am trying to redirect the destroy action to the index on destroy because all these trackers are displayed in a summarized table in index.

On my destroy action, When I redirect to tracker_url my flash notice appears correctly. But the URL has the id (as it should)

 redirect_to tracker_url, :notice => "Tracker Disabled!"
 redirect with the flash notice displaying
 /trackers/trackers/5

When i redirect to index using the redirect to below, it shows the flash notice in the url but doesn't display the flash notice in a message.

redirect_to :action => index, :notice => "Tracker Disabled!"
redirects without the flash notice
/trackers?notice=Tracker+Disabled%21

To debug, i have copied the code from index.html.erb and show.html.erb so that they are identical and still gt the flash notice as part of the url even thought they both have the render for messages in the same spot. The partial for messages is in the layouts/messages folder

<%= render 'layouts/messages' %>

Below are my show and index

def index
   authorize! :view, :silver, :message => 'Access limited to Silver Plan subscribers.'

   @new_tracker = Tracker.new
   @current_user = current_user
   @trackers_enabled = Tracker.enabled.where(:user_id => @current_user.id)
   @trackers_disabled = Tracker.disabled.where(:user_id => @current_user.id)
 end

def show
   authorize! :view, :silver, :message => 'Access limited to Silver Plan subscribers.'

  @new_tracker = Tracker.new
  @current_user = current_user
  @trackers_enabled = Tracker.enabled.where(:user_id => @current_user.id)
  @trackers_disabled = Tracker.disabled.where(:user_id => @current_user.id)
end

From my development log here is the posts after the redirect

SHOW

Redirected to /trackers/1 Completed 302 Found in 19ms (ActiveRecord: 13.8ms)

Started GET "/trackers/1" for 127.0.0.1 at 2013-09-14 11:06:06 -0500 Processing by TrackersController#show as HTML Parameters: {"id"=>"1"}

INDEX

Redirected to /trackers?notice=Tracker+Disabled%21 Completed 302 Found in 19ms (ActiveRecord: 13.5ms)

Started GET "/trackers?notice=Tracker+Disabled%21" for 127.0.0.1 at 2013-09-14 11:06:51 -0500 Processing by TrackersController#index as HTML Parameters: {"notice"=>"Tracker Disabled!"}

Was it helpful?

Solution

This line has problem

redirect_to :action => index, :notice => "Tracker Disabled!"

The notice part becomes param of #index in your code, instead of argument of redirect_to.

To fix, use named path. Suppose your index page path is trackers_path, then

redirect_to trackers_path, :notice => "Tracker Disabled!"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top