Ruby-on-rails 调用错误方法时出现“未定义的方法‘错误’为 nil:NilClass”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18136632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"undefined method `errors' for nil:NilClass" when calling on errors method
提问by tomr
I am currently teaching myself some RoR and doing the tutorial, but adding some nicer layout and stuff with bootstrap and I am running into a problem which I cant figure out.
我目前正在自学一些 RoR 并做教程,但是添加了一些更好的布局和引导程序的东西,我遇到了一个我无法弄清楚的问题。
I am trying to do the validation part (http://guides.rubyonrails.org/getting_started.html#adding-some-validation), but when I use:
我正在尝试进行验证部分(http://guides.rubyonrails.org/getting_started.html#adding-some-validation),但是当我使用:
<% @post.errors.any? %>
I get this message:
我收到这条消息:
undefined method `errors' for nil:NilClass
Extracted source (around line #9):
<legend><h1>Add Post</h1></legend>
<%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
Nothing works and I even copied and pasted the parts from the tutorial.
没有任何效果,我什至复制并粘贴了教程中的部分。
Here is the code for the view:
这是视图的代码:
<p> </p>
<div class="span6"
<fieldset>
<legend><h1>Add Post</h1></legend>
<%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'span4' %>
</div>
</div>
<div class="control-group">
<%= f.label :content, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :content, :rows => '7', :class => 'input-block-level' %>
</div>
</div>
<div class="form-actions">
<%= f.submit "Add Post", :class => 'btn btn-success' %>
<%= link_to "Cancel", posts_path, :class => 'btn', :style => 'float:right;' %>
</div>
<% end %>
</fieldset>
</div>
And my posts_controller:
还有我的posts_controller:
class PostsController < ApplicationController
def new
end
def create
@post = Post.new(params[:post].permit(:title, :content))
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.order("created_at desc")
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
What am I missing? Thanks in advance!
我错过了什么?提前致谢!
回答by deefour
You need to define @postin your newaction too.
你也需要@post在你的new行动中定义。
def new
@post = Post.new
end
You're getting the NilClasserror because @posthas no value (it's nil)when you first load the form on the newaction.
您收到NilClass错误是因为当您第一次在操作上加载表单时@post没有值(它是nil)new。
When you do the render :newin your createaction there is no problem because it's using the @postyou've defined at the top of create.
当您render :new在create操作中执行 the时没有问题,因为它使用的是@post您在create.
回答by Sunny
Update the create method in posts.controller.rbfile with the below piece of code. It worked for me.
posts.controller.rb使用以下代码更新文件中的 create 方法。它对我有用。
def create
@post = Post.new(params[:post].permit(:title, :text))
@post.save
redirect_to @post
end
回答by Gourav Thakur
In your posts_controller, add this :
在您的帖子控制器中,添加以下内容:
def new
@post = Post.new
end

