我最近在项目中开始使用ActiveAdmin,几乎所有功能都很棒,但是当与friendly_id gem一起使用时出现了问题。当在表单中使用它时,我会遇到ActiveRecord::ReadOnlyRecord异常(我认为)是因为friendly_id属性的ID是只读的:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"Rc5PmUYZt3BiLvfPQr8iCPPXlbfgjoe/n+NhCwXazNs=",
"space"=>{"name"=>"The Kosmonaut",
"address"=>"8 Sichovykh Striltsiv 24",
"email"=>"info@somedomain.com"},
"commit"=>"Update Space",
"id"=>"the-kosmonaut"} <--- culprit
我猜测最后一行是罪魁祸首,因为它是只读属性,它不在我的表单中,而是在路径中
http://localhost:5000/manage/spaces/the-kosmonaut/edit
我该如何修复它不去尝试更新ID?
ActiveAdmin中的表单如下:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :email
f.input :phone
f.input :website
end
f.inputs "Content" do
f.input :description
f.input :blurb
end
f.buttons
end
更新:这也不起作用,所以不是 friendly_id 的问题?
我尝试使用 @watson 的建议,本应该可以起作用,但仍然出现了相同的错误;-(
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"Rc5PmUYZt3BiLvfPQr8iCPPXlbfgjoe/n+NhCwXazNs=",
"space"=>{"name"=>"The Kosmonaut 23"},
"commit"=>"Update Space",
"id"=>"6933"}
http://localhost:5000/manage/spaces/6933/edit
当我使用record.readonly?在控制台中检查记录时,它返回false。
更新:删除scope_to可以解决问题。
scope_to :current_user, :unless => proc{ current_user.admin? }
唯一的问题是我需要使用scope_to,以防止用户查看他们不拥有的记录。 我猜测(因为我假设scope_to通常与has_many一起使用)我的HABTM关联会导致一些奇怪的情况?即 用户 <-- HABTM --> 空间?
app/admin/dashboard.rb
的controller
块中添加了一个空的revert_friendly_id
来修复错误。 - pix0rFoobar
类的修改结果不一致。 - Eliot Sykes