I have the following code in my controller
def create
@tv_show = TvShow.new(params[:tv_show])
respond_to do |format|
if @tv_show.save
format.html { redirect_to(tv_shows_path, :notice => 'Tv show was successfully created.') }
format.xml { render :xml => @tv_show, :status => :created, :location => @tv_show }
else
format.html { render :action => "new" }
format.xml { render :xml => @tv_show.errors, :status => :unprocessable_entity }
end
end
end
and the following in my tv_shows/index.html.erb
<div id="notice"><%= notice %></div>
but when I create a new entry the notice message does not appear after the redirect to tv_shows_path. Have anyone an idea why?
Is there any reason you're trying to use :notice
and not flash[:notice]
?
Controller:
respond_to do |format|
if @tv_show.save
format.html {
flash[:notice] = 'Tv show was successfully created.'
redirect_to tv_shows_path
}
format.xml { render :xml => @tv_show, :status => :created, :location => @tv_show }
else
format.html { render :action => "new" }
format.xml { render :xml => @tv_show.errors, :status => :unprocessable_entity }
end
end
View:
<% if flash[:notice] %>
<div id="notice"><%= flash[:notice] %></div>
<% end %>
I encountered similar 'problem' and the cause was that I was redirecting to action that in itself had another redirection. In the above case the most probable cause was that within tv_shows_path
another redirection exists.
In my case I had something like this in a filter:
redirect_to root_url, notice: 'Unauthorized access!'
And root_url
was set to point to home#index
:
# Home controller
def index
if user_signed_in? && current_user.admin?
redirect_to users_path
else
redirect_to customers_path
end
end
This second redirect_to was causing my 'unauthorized_access' notice not to show up.
The solution is to simply redirect to customers_path
immediately and not to root_url
. Hope this helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With