Here's the error I get:
我得到的错误是:
ActionView::Template::Error (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
It's referring to this code in a view partial:
它指的是视图部分中的代码:
<% flash.each do |key, value| %>
<div class="<%= key %>"><%= value %></div>
<% end %>
I set the flash message in my micropost controller:
我在微博控制器中设置了flash消息:
def create
@micropost = current_user.microposts.build(params[:micropost])
respond_to do |format|
if @micropost.save
flash[:success] = "Micropost created!"
format.html { redirect_to root_path, :notice => 'success' }
format.js
Here's my js code in views/microposts/create.js.erb:
以下是我的js代码在views/microposts/ create.s.erb:
$('div.notice').append('<%= render 'shared/flash' %>');
And the partial gets rendered dynamically in my application layout:
在我的应用程序布局中,部分被动态渲染:
<body>
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<div id= "notice"></div>
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
How do I fix this error? What am I doing wrong?
如何修正这个错误?我做错了什么?
1 个解决方案
#1
1
The problem lies in the naming of your partial file.
问题在于部分文件的命名。
Rails tries to be clever with partials and automagically exposes a local variable that shares the same name as the partial's file name. It does this because the most common use-case for partials to to bind an HTML fragment to an instance of something, ie:
Rails试图巧妙地使用部分,并自动公开与部分文件名共享相同名称的本地变量。它之所以这样做,是因为部分将HTML片段绑定到某个实例的最常见用例是:
# the following is treated as: render :partial => "frobs", :collection = @frobs
render @frobs
Then, within your _frob.html.erb
partial, you have a variable named frob
which references the current instance from @frobs
然后,在你的_frob.html。erb分部,您有一个名为frob的变量,它引用来自@frobs的当前实例
In your case, this is colliding with the flash
method exposed by ActionView
: a local variable will always trump a method name. Try re-naming your partial to something else (ie, flash_message), keeping the content the same and see if that works.
在您的示例中,这与ActionView公开的flash方法相冲突:局部变量总是胜过方法名。试着把你的部分重新命名为其他的东西(比如flash_message),保持内容不变,看看这样是否有效。
#1
1
The problem lies in the naming of your partial file.
问题在于部分文件的命名。
Rails tries to be clever with partials and automagically exposes a local variable that shares the same name as the partial's file name. It does this because the most common use-case for partials to to bind an HTML fragment to an instance of something, ie:
Rails试图巧妙地使用部分,并自动公开与部分文件名共享相同名称的本地变量。它之所以这样做,是因为部分将HTML片段绑定到某个实例的最常见用例是:
# the following is treated as: render :partial => "frobs", :collection = @frobs
render @frobs
Then, within your _frob.html.erb
partial, you have a variable named frob
which references the current instance from @frobs
然后,在你的_frob.html。erb分部,您有一个名为frob的变量,它引用来自@frobs的当前实例
In your case, this is colliding with the flash
method exposed by ActionView
: a local variable will always trump a method name. Try re-naming your partial to something else (ie, flash_message), keeping the content the same and see if that works.
在您的示例中,这与ActionView公开的flash方法相冲突:局部变量总是胜过方法名。试着把你的部分重新命名为其他的东西(比如flash_message),保持内容不变,看看这样是否有效。