2

我要NoMethodError (undefined method 'name' for nil:NilClass)加入ArticlesController#create

模型Article

class Article < ActiveRecord::Base
  attr_accessible :img_data, :tags, :title, :tag_ids, :id
  has_many :taggings
  has_many :tags, :through => :taggings
end

,并且ArticlesController#create

def create
  params[:article][:tag_ids] ||= []
  @article = Article.new params[:article]

  respond_to do |format|
    if @article.save
      format.html { redirect_to @article, notice: 'Article was successfully created.' }
      format.json { render json: @article, status: :created, location: @article }
    else
      format.html { render action: "new" }
      format.json { render json: @article.errors, status: :unprocessable_entity }
    end
  end
end

错误消息说错误发生在@article.save. 我应该怎么做才能修复错误?


我以为name是来自Tag. 模型是

 class Tag < ActiveRecord::Base
   attr_accessible :name, :id
   has_many :taggings
   has_many :articles, :through => :taggings
 end

, 和模型,Tagging

 class Tagging < ActiveRecord::Base
   attr_accessible :article_ids, :tag_ids, :tag_id, :article_id
   belongs_to :article
   belongs_to :tag
 end
4

0 回答 0