Why does Rails flash [: notice] = "msg" work where: notice => "msg" is not?
Why does Rails flash [: notice] = "msg" work where: notice => "msg" is not? A notification is displayed 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 described technique works very well:
# 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 use :notice => "" works in one case, but not in another?
I understand that I did not give you much context, but I believe that my problem is actually very simple.
ps This is similar to this question .
Redirect_to method takes two arguments as per documentation
The second argument is the key :notice .
However, in your case, 2 ruby ββcannot determine if there is one or more hashes. Only one hash is considered passed to the redirect_to method.
You can force ruby ββto pass a second hash by explicitly placing brackets around each hash:
format.html { redirect_to({:action => "show_matches", :id => @trial.id}, {:notice => "Candidate was successfully registered."}) } Case 3 works because there is no ambiguous hash situation.