模板错误:未定义方法“each” 对于 nil:NilClass

发布于 2024-07-16 14:34:55 字数 1969 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-07-23 14:34:55

您可能应该使用会话对象在操作之间传递数据。 Flash 用于在操作之间传递消息,而不是数据!

You should probably use the session object to pass data between actions. Flash is for passing messages between actions, not data!

面犯桃花 2024-07-23 14:34:55

对于该实例,您的 @features 实例变量为零。

Your @features instance variable is nil for that instance.

婴鹅 2024-07-23 14:34:55

我认为您需要将 flash.keep 放入创建操作中,因为您使用的是 redirect_to 而不是 render

来自 ActionController::Flash::FlashHash

当你需要将一个对象传递给当前操作时,你可以使用now,当当前操作完成时,你的对象将消失。

通过 now 设置的条目的访问方式与标准条目相同:flash['my-key']。

I think you need to put flash.keep in the create action since you're using redirect_to and not render.

From ActionController::Flash::FlashHash

When you need to pass an object to the current action, you use now, and your object will vanish when the current action is done.

Entries set via now are accessed the same way as standard entries: flash[‘my-key’].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文