NoMethodError:未定义的方法`name'为零:NilClass

时间:2016-03-22 06:52:08

标签: ruby-on-rails-4

我是ruby on rails并尝试通过博客Web应用程序来学习,当我尝试创建一篇文章并为其添加一个类别时,我收到了错误(NoMethodError:undefined method`name&# 39;对于nil: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

2 个答案:

答案 0 :(得分:0)

此错误是由于过滤器造成的。删除所有控制器中的before_action并尝试

答案 1 :(得分:0)

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