0

我有一个学校项目,我有点困惑如何制作标签和类别相关的帖子,所以当我在谷歌寻找一些提示时,我发现了这个帖子。所以我尝试了描述的脚手架,它工作得很好,但是当我运行服务器并尝试创建新帖子时,出现了:

ActiveModel::MassAssignmentSecurity::PostsController#create 中的错误

无法批量分配受保护的属性:类别、用户

所以我真的不知道出了什么问题,但我可以使用一些帮助。或者也许可以建议另一种方式,也许更简单的是如何使用标签和类别来搭建帖子。

非常感谢

以下是模型:

class Post < ActiveRecord::Base
  belongs_to :category
  belongs_to :user
  attr_accessible :body, :title, :category, :user
end

class Category < ActiveRecord::Base
  attr_accessible :name
end

class Serie < ActiveRecord::Base
  attr_accessible :name, :website
end

class Tag < ActiveRecord::Base
  attr_accessible :name
end

class TagsSerie < ActiveRecord::Base
  belongs_to :serie
  belongs_to :tag
  # attr_accessible :title, :body
end

class TagsPost < ActiveRecord::Base
  belongs_to :post
  belongs_to :tag
  # attr_accessible :title, :body
end

class User < ActiveRecord::Base
  attr_accessible :email, :password
end
4

3 回答 3

2

添加attr_accessible您的帖子模型:

class Post < ActiveRecord::Base
  attr_accessible :category_id, :user_id, :other_attributes_from_post_model
end
于 2013-05-10T10:41:22.507 回答
1

尝试attr_accessible :category_id, :user_id在您的帖子模型中设置。

于 2013-05-10T10:39:46.587 回答
1

By default, Rails creates the scaffolded models with all its attributes non-accessible, so they are not available to edit by an external user.

So, when you tried to create a new Post, the error message raised, as category and user are protected attributes of Post.

You should review your app/models/post.rb and the rest of your models in the same folder to define as accessible those attributes that should be editable by an external user (a web user, for instance).

class Post < ActiveRecord::Base
  attr_accessible :category_id, :user_id
end

On the other hand, the so accessible attributes are not protected any more for external edition so you should not use attr_accessible for all of them but just for ones that you will really allow to be modified externally.

于 2013-05-10T10:51:03.887 回答