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 therespond_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
How to Sort a Ruby Hash Alphabetically by Keys
Running Phantomjs from a Ruby on Rails Application
What Rails Plugins Are Good, Stable and *Really* Enhance Your Code
How to Read a File's Modification Date with Ruby
Ruby: How to Calculate a Path Relative to Another One
Add_Foreign_Key VS Add_Reference in Rails
Are There Any Good Mutation Testing Tools for Ruby 1.9 and Rspec2
Best Way to Handle Data Attributes in Slim
Rails: Difference Between Env.Fetch() and Env[]
Decoding Facebook's Signed Request in Ruby/Sinatra
How to Tell If I'm Running from Jruby VS. Ruby
How to Get Rspec to Run All Tests Nested Under a Folder
Rspec Any_Instance Deprecation: How to Fix It
Openssl Error Installing Ruby 2.0.0-P195 on MAC with Rbenv
What's the Cleanest Way to Override Activerecord's Find for Both Models and Collections
Unpacking/Freezing Gems into a Non-Rails Ruby App
How to Resolve Deprecation Warnings for Openssl::Cipher::Cipher#Encrypt