2016-03-22 100 views
0

我是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 –

回答

0

此錯誤是因爲過濾器的。刪除所有控制器中的before_action,然後嘗試

0

問題是Ruby版本,將版本更改爲ruby 2.1,現在工作正常。

相關問題