1

我想在我的文章中添加作者并且:

unknown attribute: author_id

控制器:

    def create
  @article = current_user.articles.new(params[:article])
  @article.save
flash.notice = "Article '#{@article.title}' Posted!"
  redirect_to article_path(@article)

型号1:

class Author < ActiveRecord::Base
  authenticates_with_sorcery!
  has_many :articles
  validates_confirmation_of :password, message: "should match confirmation", if: :password
end

型号2:

class Article < ActiveRecord::Base
   attr_accessible :title, :body, :tag_list, :image 
belongs_to :author
has_many :taggings
has_many :tags, through: :taggings
has_attached_file :image, :default_url => '/no_image.jpg'

end

可悲的是我不知道出了什么问题

////

在文章模型中你应该使用 author_id 因为作者有很多文章。user_id 将用于将每篇文章指向特定作者

现在我得到了这个:

uninitialized constant Article::AuthorId
4

4 回答 4

1

您需要通过迁移将 author_id 添加到您的数据库表中:

class AddAuthorToArticle < ActiveRecord::Migration
  def change
    add_column :articles, :author_id, :integer
  end
end
于 2013-06-10T22:08:23.640 回答
1

您可能需要将 author_id 添加到您的 attr_accessible 列表中。

于 2013-06-10T17:58:33.847 回答
1

在文章模型中你应该使用 author_id 因为作者有很多文章。user_id 将用于将每篇文章指向特定作者

于 2013-06-10T18:00:43.693 回答
1

添加authors_attributes到您attr_accessible的作者模型中。确保您在迁移中也有引用列。

在您的文章迁移中,您应该有这样的一行:

t.references :author
于 2013-06-10T18:49:55.970 回答