NoMethodError:未定义的方法名称为零:NilClass

问题描述:

我是Ruby on Rails的新手,尝试通过做一个博客Web应用程序学习,当我尝试创建一篇文章并包含一个类别时,我得到了错误(NoMethodError:未定义方法`name'为nil:NilClass)。 当我不包括类别,只是创建文章的东西似乎工作正常。我不确定什么可能是错的,有人可以帮忙吗?NoMethodError:未定义的方法名称为零:NilClass

这里是我的数据库架构:

ActiveRecord::Schema.define(version: 20160322060108) do 

create_table "article_categories", force: true do |t| 
    t.integer "article_id" 
    t.integer "category_id" 
end 

    create_table "articles", force: true do |t| 
    t.string "title",    limit: nil 
    t.text  "description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "user_id" 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    end 

    create_table "categories", force: true do |t| 
    t.string "name" 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    t.datetime "created_at" 
    end 

    create_table "users", force: true do |t| 
    t.string "username",  limit: nil 
    t.string "email",   limit: nil 
    t.string "password_digest", limit: nil 
    t.boolean "admin",      default: false 
    end 
end 

类模型:

class Category < ActiveRecord::Base 

    has_many :article_categories 
    has_many :articles, through: :article_categories 

    validates :name, presence: true, length:{ minimum: 3, maximum: 25} 
    validates_uniqueness_of :name 

    has_attached_file :avatar, styles: { medium: "270x179>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 

end 

文章型号:

class Article < ActiveRecord::Base 
    belongs_to :user 
    has_many :article_categories 
    has_many :categories, through: :article_categories 
    validates :title, presence: true, length:{ minimum: 3, maximum: 60} 
    validates :description, presence: true, length:{ minimum: 10} 

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/missing.jpg" 
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 
end 

article_category型号:

class ArticleCategory < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :category 
end 

articles_controller:

class ArticlesController < ApplicationController 

    before_action :set_article, only: [:edit, :update, :show, :destroy] 
    before_action :require_user, except: [:index, :show] 
    before_action :require_same_user, only: [:edit, :update, :destroy] 

    def index 
    @articles = Article.paginate(page: params[:page], per_page: 5) 
    end 

    def new 
    @article = Article.new 
    end 

    def edit 
    end 

    def create 
    @article = Article.new(article_params) 
    @article.user = current_user 
    if @article.save 
     flash[:success] = "Article was successfully created" 
     redirect_to article_path(@article) 
    else 
     render 'new' 
    end 
    end 

    def update 
    if @article.update(article_params) 
     flash[:success] = "Article was successfully updated" 
     redirect_to article_path(@article) 
    else 
     render 'edit' 
    end 
    end 

    def show 
    end 

    def destroy 
    @article.destroy 
    flash[:danger] = "Article was successfully deleted" 
    redirect_to articles_path 
    end 

    private 
    def set_article 
     @article = Article.find(params[:id]) 
    end 

    def article_params 
     params.require(:article).permit(:title, :description, :avatar, category_ids: []) 
    end 

    def require_same_user 
     if current_user != @article.user and !current_user.admin? 
     flash[:danger] = "You can only edit or delete your own articles" 
     redirect_to root_path 
     end 
    end 

end 

和categories_controller

class CategoriesController < ApplicationController 

    before_action :require_admin, except: [:index, :show] 

    def index 
    @categories = Category.paginate(page: params[:page], per_page: 9) 
    end 

    def new 
    @category = Category.new 
    end 

    def edit 
    @category = Category.find(params[:id]) 
    end 

    def update 
    @category = Category.find(params[:id]) 
    if @category.update(category_params) 
     flash[:success] = "Category name was successfully updated" 
     redirect_to category_path(@category) 
    else 
     render 'edit' 
    end 
end 

    def create 
    @category = Category.new(category_params) 
    if @category.save 
     flash[:success] = "Category was created successfully" 
     redirect_to categories_path 
    else 
     render 'new' 
    end 
    end 

    def show 
    @category = Category.find(params[:id]) 
    @category_articles = @category.articles.paginate(page: params[:page], per_page: 5) 
    end 

    private 
    def category_params 
     params.require(:category).permit(:name, :avatar) 
    end 

    def require_admin 
     if !logged_in? && (logged_in? and !current_user.admin?) 
     flash[:danger] = "Only admins can perform that action" 
     redirect_to categories_path 
     end 
    end 

end 

Terminal

+0

你从哪里得到错误?你可以把日志。 –

+0

为什么不把'article_id'字段添加到你的类别表中,这样你就可以将你的表关联起来 – Lymuel

+0

刚刚添加了终端图片aldrien.h –

此错误是因为过滤器的。删除所有控制器中的before_action,然后尝试

问题是Ruby版本,将版本更改为ruby 2.1,现在工作正常。