Why does Rails flash[:notice] =”msg” work where :notice => “msg” doesn’t? The notice displays if I use the following code:
# Case 1 (this works)
flash[:notice] = 'Candidate was successfully registered.'
format.html { redirect_to :action => "show_matches", :id => @trial.id }
This does not work:
# Case 2 (this doesn't)
format.html { redirect_to :action => "show_matches", :id => @trial.id, :notice => "Candidate was successfully registered."}
But in other areas of my application, the above technique works just fine:
# Case 3 (this works)
format.html { redirect_to @candidate, :notice => 'Candidate was successfully created.' }
My layout includes:
<section id="middle_content">
<% flash.each do |key, value| -%>
<div id="info_messages" class="flash <%= key %>"><%= value %></div>
<br/>
<% end -%>
<%= yield -%>
</section>
So my question is why does using :notice => "" work in one case but not the other?
I realize I have not given you much context, but my sense is that my issue is actually very simple.
p.s. This seems similar to this question.
The redirect_to method takes two arguments according to the documentation
The second argument is where the
:noticekey is to be put.However, in your Case 2, ruby can’t tell if there is one or more hashes involved. Only one hash is considered to be passed to the redirect_to method.
You can force ruby to pass a second hash by explicitly setting brackets around each hash :
The case 3 works because there is no ambiguous hash situation here.