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.
I just had the same issue and the error was so silly but sometimes unnotable.
I have in my application layout the following code:
<div id="content" >
<div class="wrapper">
<% flash.each do |name, msg| %>
<% content_tag :div, msg, class: "flash #{name}"%>
<% end %>
<%= yield %>
</div>
</div>
Now, can you see why I was unable to see my flash message?
Yup! You should check if you put the =
sign here:
<%= content_tag :div, msg, class: "flash #{name}"%>
The reason why the code didn't work was a problem with my authentication code... After I implemented my new way of authentication from scratch the code above is working.