我最近在一个项目中开始使用 ActiveAdmin,几乎一切都很好,但是在将它与 friendly_id gem 结合使用时遇到了问题。我的表单收到 ActiveRecord::ReadOnlyRecord [我相信],因为友好 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
我猜最后一行是罪魁祸首,因为它是一个只读属性,它不在我的表单中,而是在 PATH 中
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检查控制台中的记录时?它返回假
更新更新:删除 scope_to 可以解决问题。
scope_to :current_user, :unless => proc{ current_user.admin? }
唯一的问题是我需要 scope_to 来防止用户看到他们不拥有的记录。我的猜测是(因为我假设 scope_to 通常与 has_many 一起使用)我的 HABTM 关联会导致一些奇怪?即用户 <-- HABTM --> 空间?