页面中没有方法错误#home [英] NoMethodError in Pages#home
问题描述
我正在阅读 rails 3 的 railtutorial.org 在线书籍.我已经完成了第 11 章的大部分内容,在那里我们添加了提交微博的功能.添加适当的代码后,我无法呈现页面.以下是返回的错误:
>
Pages#home 中的 NoMethodError
显示 c:/rails_projects/sample_app/app/views/shared/_error_messages.html.erb 其中第 >#1 行:
当你没想到的时候,你得到了一个 nil 对象!您可能希望有一个 ActiveRecord::Base 实例.评估 nil.errors 时发生错误提取的源代码(围绕第 1 行):
1:<% if @user.errors.any?%>
2:
3:
<%=pluralize(@user.errors.count, "error") %>
4:
禁止这种<%= object.class.to_s.underscore.humanize.downcase %>
模板包含痕迹:app/views/shared/_micropost_form.html.erb, >app/views/pages/home.html.erb
如果我从 app\views\shared_micropost_form.html.erb 中删除以下行,页面将正确呈现<%= 渲染 'shared/error_messages', :object => f.object %>
感谢任何帮助.
这是因为你将一个变量 object
传递到你的局部,但是在局部你试图使用一个名为@user
.将该部分中 @user
的每个实例更改为 object
,它会正常工作.
1:<% if object.errors.any?%>2:<div id="error_explanation">3:<h2><%=pluralize(object.errors.count, "error") %>4:禁止这种<%= object.class.to_s.underscore.humanize.downcase %>
更新: 只是为了澄清一下,上面的答案是假设设置@user 变量有问题,但没关系.当你说 :object =>f.object
在您的渲染调用中,您告诉 render
获取此表单所基于的对象,并将其发送到部分 - 使用变量名称 object代码>.
将错误代码重构为共享部分的全部意义在于它将被多个表单用于不同的模型.在部分中,您不能说 @user
,因为您将在所有其他模型中使用相同的部分.这就是为什么将部分中的代码更改为使用更通用的变量名称,object
.
I'm working through the railtutorial.org online book for rails 3.
I've made it through most of chapter 11, where we add the ability to submit a micropost. After adding the appropriate code, I'm unable to render the page. The following is the error returned:
>
NoMethodError in Pages#home
Showing c:/rails_projects/sample_app/app/views/shared/_error_messages.html.erb where line >#1 raised:
You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.errors Extracted source (around line #1):
1:
<% if @user.errors.any? %>
2:<div id="error_explanation">
3:<h2><%= pluralize(@user.errors.count, "error") %>
4:prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
Trace of template inclusion: app/views/shared/_micropost_form.html.erb, >app/views/pages/home.html.erb
The page will render correctly if I remove the following line from app\views\shared_micropost_form.html.erb <%= render 'shared/error_messages', :object => f.object %>
Any help is appreciated.
it's because you're passing a variable object
into your partial, but in the partial you're trying to use a variable called @user
. Change each instance of @user
in that partial to object
and it will work fine.
1:<% if object.errors.any? %>
2:<div id="error_explanation">
3:<h2><%= pluralize(object.errors.count, "error") %>
4:prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
UPDATE: Just to clarify, the answers above are assuming there's a fault with setting the @user variable, but it's fine. When you say :object => f.object
in your render call, you're telling render
to take the object that this form is based on, and send it to the partial - with the variable name object
.
The whole point of refactoring the error code into a shared partial is that it will be used by multiple forms, for different models. Inside the partial you can't say @user
because you will be using this same partial for all your other models. That's why the code in the partial is changed to use a more generic variable name, object
.
这篇关于页面中没有方法错误#home的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!