模板错误:未定义方法“each” 对于 nil:NilClass
我的 Rails 应用程序在本地运行良好。 但是,一旦我将它放在服务器上并处于生产模式,我就会收到此错误:
ActionView::TemplateError (undefined method `each' for nil:NilClass) on line #7 of app/views/admin/confirm.rhtml:
4: <br>Description:
5: <br><%= @description %>
6: <br>Features:
7: <% @features.each do |feature| %>
8: <br><%= feature.humanize %>
9: <% end %>
10: <br>Role data:
app/views/admin/confirm.rhtml:7
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/template.rb:73:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:256:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:367:in `_render_with_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:254:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1174:in `render_for_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:896:in `render_without_benchmark'
有人知道这意味着什么吗?
编辑:好的我发现@features 为零。 但我不知道情况如何。 在我的创建操作中,我有:
flash[:name] = params[:name]
flash[:description] = params[:description]
flash[:role_data] = params[:role_data]
flash[:user_data] = params[:user_data]
flash[:features] = params[:features]
flash[:theme] = params[:theme]
redirect_to :action => "confirm"
然后在我的确认操作中,我有:
def confirm
@title = "Create a new simulation"
@features = flash[:features]
@name = flash[:name]
@description = flash[:description]
@role_data = flash[:role_data]
@user_data = flash[:user_data]
@theme = flash[:theme]
flash.keep
end
my Rails app works fine locally. But once I put it on a server and in production mode, I get this error:
ActionView::TemplateError (undefined method `each' for nil:NilClass) on line #7 of app/views/admin/confirm.rhtml:
4: <br>Description:
5: <br><%= @description %>
6: <br>Features:
7: <% @features.each do |feature| %>
8: <br><%= feature.humanize %>
9: <% end %>
10: <br>Role data:
app/views/admin/confirm.rhtml:7
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/template.rb:73:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:256:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:367:in `_render_with_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:254:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1174:in `render_for_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:896:in `render_without_benchmark'
Anyone have any idea what it means?
EDIT: OK I found out @features is nil. But I don't know how it is. In my create action I have:
flash[:name] = params[:name]
flash[:description] = params[:description]
flash[:role_data] = params[:role_data]
flash[:user_data] = params[:user_data]
flash[:features] = params[:features]
flash[:theme] = params[:theme]
redirect_to :action => "confirm"
Then in my confirm action I have:
def confirm
@title = "Create a new simulation"
@features = flash[:features]
@name = flash[:name]
@description = flash[:description]
@role_data = flash[:role_data]
@user_data = flash[:user_data]
@theme = flash[:theme]
flash.keep
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能应该使用会话对象在操作之间传递数据。 Flash 用于在操作之间传递消息,而不是数据!
You should probably use the session object to pass data between actions. Flash is for passing messages between actions, not data!
对于该实例,您的 @features 实例变量为零。
Your @features instance variable is nil for that instance.
我认为您需要将
flash.keep
放入创建操作中,因为您使用的是redirect_to
而不是render
。来自 ActionController::Flash::FlashHash
I think you need to put
flash.keep
in the create action since you're usingredirect_to
and notrender
.From ActionController::Flash::FlashHash