Ruby on Rails noob在这里参加了RoR.org上的入门教程.我在“5.11添加一些验证”部分,并在尝试刷新/ posts / new页面时抛出错误:
错误消息:
NoMethodError in PostsController#new undefined method ` validates' for #<Class:0x0000000414fab0> Extracted source (around line #2): 1 class Post < ActiveRecord::Base 2 validates :title,presence: true,3 length: { minimum: 5 } 4 end
我的帖子.rb:
class Post < ActiveRecord::Base validates :title,length: { minimum: 5 } end
我的posts_controller.rb:
class PostsController < ApplicationController def index @posts = Post.all end def new @post = Post.new end def create @post = Post.new(params[:post].permit(:title,:text)) if @post.save redirect_to @post else render 'new' end end def show @post = Post.find(params[:id]) end private def post_params params.require(:post).permit(:title,:text) end end
Sidenote – FWIW,在尝试诊断时,我从posts_controller.rb中删除了@ post = Post.new,并在刷新时出现此错误:
undefined method `errors' for nil:NilClass Extracted source (around line #3): <h1>New Post</h1> <%= form_for :post,url: posts_path do |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count,"error") %> prohibited this post from being saved:</h2> <ul>
找不到new.html.erb部分的任何问题,所以我转向社区寻求指导.谁知道我哪里出错了?在此先感谢您的帮助.