Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails应用程序中的命名错误,方法';错误';未定义_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails应用程序中的命名错误,方法';错误';未定义

Ruby on rails Rails应用程序中的命名错误,方法';错误';未定义,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在尝试使用创建Rails博客应用程序 我陷入了验证用户输入的字段是否为空的步骤。但是,如果我尝试传递空字段,则会出现以下错误: NoMethodError in Posts#create Showing C:/Users/irowe/Documents/GitHub/Rails-Blog/blog/app/views/posts/new.html.erb where line #3 raised: undefined method `errors' for nil:NilClass 这是我

我正在尝试使用创建Rails博客应用程序

我陷入了验证用户输入的字段是否为空的步骤。但是,如果我尝试传递空字段,则会出现以下错误:

NoMethodError in Posts#create
Showing C:/Users/irowe/Documents/GitHub/Rails-Blog/blog/app/views/posts/new.html.erb where line #3 raised:
undefined method `errors' for nil:NilClass
这是我的邮政管理员:

class PostsController < ApplicationController
def index
  @posts = Post.all.order(created_at: :desc)
end
def show
end
def new
@post = Post.new
end
def create
  p = Post.new(title: params[:post][:title], content: params[:post][:content])
  if p.save
    flash[:notice] = 'Successfully created a new post!'
    redirect_to root_path
  else
    flash[:alert] = 'Something went awry...'
    render :new
  end
end
end
class PostsController
还有我的“新帖子”观点

新职位


以及后模式

class Post < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :content
before_validation :preval
private
  def preval
  if self.title
    self.title = self.title.strip
  end
  if self.content
    self.content = self.content.strip
  end
end
end
class Post

我看到过其他类似的答案,但他们只是建议确保
post
不是
nil
,我已经确定不是。关于如何确保验证工作顺利进行,有什么想法吗?我对Rails完全陌生。

问题在于您的
创建操作。在其中,您使用局部变量
p
来存储
Post
。但是,当验证失败时,您的代码将呈现新的视图。该视图正在查找实例变量
@post
。因此,只需将代码更改为:

@post = Post.new(title: params[:post][:title], content: params[:post][:content])
  if @post.save

问题在于您的
create
操作。在其中,您使用局部变量
p
来存储
Post
。但是,当验证失败时,您的代码将呈现新的视图。该视图正在查找实例变量
@post
。因此,只需将代码更改为:

@post = Post.new(title: params[:post][:title], content: params[:post][:content])
  if @post.save

我猜当你打开
表单时,你不会收到错误,但只有当你提交表单并且表单有错误时才会收到错误

这是因为您在此处再次呈现
新的
表单:

  flash[:alert] = 'Something went awry...'
  render :new
render
只是呈现表单,它不会重定向到
new
操作。
create
操作中,您从不设置
post

一个简单的解决方法是更改这两行

p = Post.new(title: params[:post][:title], content: params[:post][:content])
if p.save
为此:

@post = Post.new(title: params[:post][:title], content: params[:post][:content])
if @post.save

我猜当你打开
表单时,你不会收到错误,但只有当你提交表单并且表单有错误时才会收到错误

这是因为您在此处再次呈现
新的
表单:

  flash[:alert] = 'Something went awry...'
  render :new
render
只是呈现表单,它不会重定向到
new
操作。
create
操作中,您从不设置
post

一个简单的解决方法是更改这两行

p = Post.new(title: params[:post][:title], content: params[:post][:content])
if p.save
为此:

@post = Post.new(title: params[:post][:title], content: params[:post][:content])
if @post.save

您必须将本地变量更改为实例变量。要使实例变为变量,只能添加
@
。这意味着可以在新的.html.erb文件中使用该变量。例如:

def create
  @post = Post.new(title: params[:post][:title], content: params[:post][:content])
  if @post.save
    flash[:notice] = 'Successfully created a new post!'
    redirect_to root_path
  else
    flash[:alert] = 'Something went awry...'
    render :new
  end
end
这是rails指南中的好教程。
我希望本教程能对您有所帮助。

您必须将变量local更改为变量instances。要使实例变为变量,只能添加
@
。这意味着可以在新的.html.erb文件中使用该变量。例如:

def create
  @post = Post.new(title: params[:post][:title], content: params[:post][:content])
  if @post.save
    flash[:notice] = 'Successfully created a new post!'
    redirect_to root_path
  else
    flash[:alert] = 'Something went awry...'
    render :new
  end
end
这是rails指南中的好教程。
我希望本教程能对您有所帮助。

NilClass

如果Rails试图操作一个不存在的对象,那么您得到的错误是标准的

for nil:NilClass
对于Ruby,没有
Nil
——它将其转换为
NilClass
对象。因此,如果您试图在此对象上调用
errors
方法,它将引发异常

此行出现错误:

<% @post.errors.full_messages.each do |msg| %>
有了这些,您需要做几件事:


实例变量

您需要创建一个,然后将在视图中提供:

def create
  @post = Post.new post_params
  if @post.save
这应该允许您的视图访问
@post
变量(它不会这样做,因为它只是作用域中的局部变量)

有条件的

第二,您可能希望使
错误
调用有条件:

<% if @post.errors.any? %>
   <% @post.errors.full_messages.each do |msg| %>


这将使它为您工作。

NilClass

如果Rails试图操作一个不存在的对象,那么您得到的错误是标准的

for nil:NilClass
对于Ruby,没有
Nil
——它将其转换为
NilClass
对象。因此,如果您试图在此对象上调用
errors
方法,它将引发异常

此行出现错误:

<% @post.errors.full_messages.each do |msg| %>
有了这些,您需要做几件事:


实例变量

您需要创建一个,然后将在视图中提供:

def create
  @post = Post.new post_params
  if @post.save
这应该允许您的视图访问
@post
变量(它不会这样做,因为它只是作用域中的局部变量)

有条件的

第二,您可能希望使
错误
调用有条件:

<% if @post.errors.any? %>
   <% @post.errors.full_messages.each do |msg| %>


这应该可以让它为您工作。

实际上,您正在谈论的是实例变量。全局变量以
$
符号开头。请看这里,了解有关此主题的详细信息:谢谢Alexey Shein。这正是我想说的。事实上,你说的是实例变量。全局变量以
$
符号开头。请看这里,了解有关此主题的详细信息:谢谢Alexey Shein。这正是我想说的。