Rails Redirect_To :Back Not Working

rails redirect_to :back not working

Rails 5 has redirect_back, instead of redirect_to :back. It was changed as it used to raise an exception when request's HTTP_REFERER was not present.

So use this:

redirect_back fallback_location: root_path

You can change root_path to something else as per your requirements.

Rails: redirect_to:back not redirecting in spite of showing correct logs

Your problem is that your are redirecting only the XHR request made by javascript -- the page shown to the user remains unaffected.

To look at this in more detail, when your form is submitted, you trigger some code. This code makes an HTTP post to the server. The server does stuff, then redirects that request. Your javascript follows that redirect behind the scenes, and loads the new page. All of this is happening hidden from the user, only affecting the HTTP request that javascript fired off, not the main page itself.

In order to make the main page window change, you'll have to explicitly tell the page go somewhere else using window.location.

For example:

$http.post("/products/#{product.id}/review/new",$scope.review).success((data) ->
...
window.location = data.redirect_url

And in the controller:

render :json, {status: "duplicate", redirect_url: "/product/2"}

ruby on rails redirect_to(:back) not working

I think it's behaving as intended:

:back - Back to the page that issued the request. Useful for forms that are triggered from multiple places.

Redirect back with errors

Generally when there is an error in the create action, you would render the "new" view instead of redirecting back. In your case, you could try redirecting to the streets show path, and passing the @house attributes as query params. Then in StreetsController#show, pass the house params as an argument to build.

rails - redirect back unless previous page was artist page

Perhaps request.referer is what you're looking for? Then you can say something like:

if request.referer == artist_url(track.artist)
redirect_to [track.artist, track]
else
redirect_to :back
end

rails: redirect_to rendering but not reloading new page

Because you are issuing an AJAX call (remote: true on your form).

You have the following options:

  • Add format.js to the controller
  • Remove remote: true from the form definition and the respond_to from the controller:
def update
if @business_category.update_attributes(business_category_params)
redirect_to admin_business_categories_path
else
render :edit
end
end

Ruby on Rails: redirect_to not working after create and save

Just replace this part of your code:

  if success
flash[:success] = message
redirect_to slider_path
else
flash[:error] = message
end
redirect_to root_path

with this:

  if success
flash[:success] = message
redirect_to slider_path
else
flash[:error] = message
redirect_to root_path
end


Related Topics



Leave a reply



Submit