我要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