Reputation: 4492
From my Rails controller, in the create
method I want to do
redirect_to @post(:notice => "Post successful', :status => "Success!")
and access the parameters in show.html.erb
by using <%= notice %>
and <%= status %>
But it doesn't work. How can I fix it?
routes.rb
file:
Archive::Application.routes.draw do
resources :posts
root :to => "Posts#new"
end
Upvotes: 0
Views: 155
Reputation: 1296
The reason it's probably not working is that you are overriding the HTTP Status Code that is returned as part of the response. This status code, which is set by setting the :status
in the redirect_to syntax, is used by the browser to determine it's particular action. You can read more about the different status codes here W3C HTTP Status Codes.
Long story short, in order for a successful redirect to occur, you need to have a 3XX
code for the browser to look at the location header in the HTTP response, and load the URL specified there. If you want the redirect to work properly, you can use the following syntax below.
redirect_to @post, :notice => "Post successful"
If for some reason you want to set it manually, you can do it like so:
redirect_to @post, :notice => "Post successful", :status => 301
There are also some symbols you can use in lieu of the numeric status codes, which you can read about in the Rails documentation for redirect_to.
Upvotes: 2
Reputation: 2563
Did you try using:
redirect_to post_path , :notice => "Post successful", :status => "Success!"
Upvotes: 0