ndemoreau
ndemoreau

Reputation: 3869

rails redirect_to html format doesn't work

this action doesn't work:

redirect_to request.referer, :format => "html", :alert => exception.message

When it's redirecting from a controller action in js format, the redirect is in js as well instead of being in html format.

Any clue why? Thx!

PS: I tried :format => :html as well

EDIT FOR COMPLETENESS

The redirect occurs after a cancan autorization via this application_controller method

   if request.referer
      logger.debug "Rescueing Cancan"
      redirect_to request.referer, :format => :html, :alert => exception.message
    else
      redirect_to root_path, :alert => exception.message, :format => :html
    end
  end

This is my log:

Started GET "/challenges/ground-floor/tasks" for 127.0.0.1 at 2012-12-06 11:17:06 +0100
Processing by ChallengesController#tasks as JS
  Parameters: {"id"=>"ground-floor"}
  Projectmilestone Load (0.6ms)  SELECT "projectmilestones".* FROM "projectmilestones" WHERE "projectmilestones"."id" = 790 LIMIT 1
Rescueing Cancan
Redirected to http://127.0.0.1:3000/challenges/ground-floor?subaction=pmdocuments&page=1
Completed 302 Found in 105ms (ActiveRecord: 1.6ms)


Started GET "/challenges/ground-floor?subaction=pmdocuments&page=1" for 127.0.0.1 at 2012-12-06 11:17:06 +0100
Processing by ChallengesController#show as JS

Upvotes: 2

Views: 3051

Answers (1)

maček
maček

Reputation: 77778

Try using :back as in

redirect_to :back, alert: exception.message

From the docs on redirect_to:

:back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])

Upvotes: 2

Related Questions