我最近在一個項目中開始使用ActiveAdmin,幾乎所有的工作都很好,但是在與friendly_id gem結合使用時遇到問題。我得到的ActiveRecord :: ReadOnlyRecord拋出我的,因爲friendly_id屬性,其ID的形式[我相信]是隻讀的:使用ActiveAdmin和Friendly_id時ActiveRecord :: ReadOnlyRecord
{"utf8"=>"âœ「",
"_method"=>"put",
"authenticity_token"=>"Rc5PmUYZt3BiLvfPQr8iCPPXlbfgjoe/n+NhCwXazNs=",
"space"=>{"name"=>"The Kosmonaut",
"address"=>"8 Sichovykh Striltsiv 24",
"email"=>"[email protected]"},
"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
UPDATE:這不工作,要麼所以它不是friendly_id?
我試着使用@華生的建議,這應該有工作,但仍然得到了同樣的錯誤;-(
{"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 - >空間?
我可能是錯誤的,我的假設,你的解決方案應該有工作,但我還是不能修改記錄。我改變了這個問題來反映這一點。 – holden
Friendly_id似乎是一個紅鯡魚。如果沒有我的current_user scope_to,它可以正常使用friendly_id ... ;-( – holden
我可以使用friendly_id和我的上述方法修改記錄,並且已經用我認爲與friendly_id無關的內容更新了問題問題,但與授權有關如果您需要授權,我建議您看看CanCan –