为什么 Rails flash[:notice] ="msg" 工作,而 :note => "msg" 不起作用?如果我使用以下代码,则会显示通知:
# Case 1 (this works)
flash[:notice] = 'Candidate was successfully registered.'
format.html { redirect_to :action => "show_matches", :id => @trial.id }
这不起作用:
# Case 2 (this doesn't)
format.html { redirect_to :action => "show_matches", :id => @trial.id, :notice => "Candidate was successfully registered."}
但是在我的应用程序的其他领域,上述技术工作得很好:
# Case 3 (this works)
format.html { redirect_to @candidate, :notice => 'Candidate was successfully created.' }
我的布局包括:
<section id="middle_content">
<% flash.each do |key, value| -%>
<div id="info_messages" class="flash <%= key %>"><%= value %></div>
<br/>
<% end -%>
<%= yield -%>
</section>
所以我的问题是,为什么使用:notice => ""
在一种情况下有效,而在另一种情况下无效?
意识到我没有给你太多的背景,但我的感觉是我的问题实际上很简单。
附言这似乎与这个问题相似。
redirect_to 方法根据文档需要两个参数
第二个参数是放置:notice
键的位置。
但是,在您的案例 2 中,ruby 无法判断是否涉及一个或多个哈希。只有一个哈希被视为传递给 redirect_to 方法。
您可以通过在每个哈希两边显式设置括号来强制 ruby 传递第二个哈希:
format.html { redirect_to({:action => "show_matches", :id => @trial.id}, {:notice => "Candidate was successfully registered."}) }
案例 3 有效,因为这里没有模棱两可的哈希情况。