NoMethodError(未定义的方法`name'代表nil:NilClass)Rails3.2

时间:2013-01-21 16:20:24

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.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

0 个答案:

没有答案