Rails flash :notice doesn't work
I have this code :
def create
login(params[:email], params[:password])
if current_user
flash[:notice] = "Welcom开发者_C百科e back #{current_user.email}"
return redirect_to_first_page
else
flash[:notice] = "Email or password is wrong. Try again !"
redirect_to root_url
end
end
when the login is successful the flash is set and the redirect to the first page is made. This part is working. The second part is not setting the flash notice message. Then when the page is displayed no message from flash is show. What is different i've try to have
return redirect_to root_url
but nothing still not showing anything. In my controller i have a helper like flash_notice all it does is return flash[:notice]. This is because the flash is always empty in a view but accessible in controller. In the view i have just one line :
<%= flash_notice %>
I'm using rails 3.1
Chris Drappier is correct, the flash hash is current for one request only. You can invoke a "keep" method with
flash.keep[:notice]="This message will persist"
Personally, I like to keep the flash in question in params when needed. The gory details are here:
http://guides.rubyonrails.org/action_controller_overview.html#the-flash
精彩评论