我正在使用具有JSON API的Rails 3.2.2應用程序,並且我使用CLI客戶機來插入一些數據。它的工作原理除了作者 模型。當我嘗試創建一個新的後(POST belongs_to的:作者和 作者的has_many:帖子)我得到以下錯誤:即使我使用attr_accessible也不能批量分配受保護的屬性
<h1>
ActiveModel::MassAssignmentSecurity::Error in PostsController#create
</h1>
<pre>Can't mass-assign protected attributes: name</pre>
我做了很多的課題研究,但我沒有找到有效的解決方案 : - (
我用attr_accessible避免MassAssignent錯誤,它適用於所有 其他車型,但不是因爲「作者」的名稱屬性
以下是作者模型:
class Author < ActiveRecord::Base
attr_accessible :name, :email
extend FriendlyId
friendly_id :name, use: :slugged
# some validations
has_many :posts
#authlogic
acts_as_authentic
# some stuffs
end
其實,我有殘疾whitelist_attributes,它解決了我的問題 但我想,這是不是這樣做(也許 不是一個好主意)的便捷方式。
我的問題是:爲什麼attr_accessible在這裏不起作用?如何在不禁用白名單的情況下解決問題 ?
謝謝
瑞文
編輯:
它創建新的崗位的方法:
def create
@post = Post.new(params[:post])
@post.author = current_author
# respond to etc.
end
current_author發現使用給定的API密鑰的作者。
看看其他車型使用'friendly_id'?它是否有可能「解除」attr_accessible所做的一些事情?沒有檢查過,也不知道它 - 只是一個想法。 – 2012-04-27 17:31:28
謝謝你的回答。幾乎所有其他模型使用friendly_id – R3v4n 2012-04-27 17:35:56
無賴;好的:)在類聲明中的相同順序? – 2012-04-27 17:36:48