我不明白为什么我的rails视图不能识别flash [:notice]或flash [:error]。我不断收到有关正在呈现的局部视图的以下错误。具体的错误是:Rails flash [:notice] always nil
ActionView :: Template :: Error(你没有对象,当你没有料到它! 你可能已经期待一个Array的实例 评估nil时出错。 ):
在我的控制器我有
def index
@organisms = Organism.all
flash[:error] = "test"
flash[:notice] = "test"
respond_to do |format|
format.html
format.json { render :json => @organisms }
end
end
在我的index.html.erb文件I呈现出的部分通过:
<%= render "shared/flash" %>
该部分具有以下代码。
<div id="flashes">
<% if flash[:notice] %>
<p id="flash_notice" class="messages notice"><%= flash[:notice] %></p>
<%= javascript_tag "$('#flash_notice').effect('highlight',{},1000);" %>
<% end %>
<% if flash[:error] || flash[:errors] %>
<p id="flash_errors" class="messages errors"><%= flash[:error] || flash[:errors] %></p>
<%= javascript_tag "$('#flash_errors').effect('highlight',{},1000);" %>
<% end %>
<% flash[:error] = flash[:errors] = flash[:notice] = nil %>
</div>
然而,如果代替使该部分I中<%扔=通知%>它呈现出通知。
如果我将部分代码粘贴到index.html.erb文件的顶部,它会正确呈现。因此,我认为我错误地渲染了局部视图?
任何帮助,非常感谢。谢谢!
你错过了关键部分:你如何渲染局部。另外,不要在底部取消闪光灯。 Rails已经为你做了这些。 –
感谢您的信息。我添加了如何渲染部分。 –
你不需要做这个<%flash [:error] = flash [:errors] = flash [:notice] = nil%> –