試圖嵌套自定義屬性,資料(一個Mongoid文件),添加到我的色器件用戶類。當提交設計註冊表時,它應該同時創建一個用戶和相應的配置文件對象。軌道4 /設計/ MongoDB的:「不允許的參數」使用自定義屬性和強參數
我想最終的結果看起來像這樣在我的MongoDB:
用戶:
{
# Devise fields:
"email": "[email protected]",
...
# Custom field
"profile" : "<object_id>"
}
簡介:
{
"first_name": "Dave",
....
}
不幸的是,我收到了每當我提交我的註冊時,都會在我的控制檯中對此進行調查它成功創建了一個用戶,但未能創建關聯的配置文件。
Started POST "/" for 127.0.0.1 at 2013-04-20 23:37:10 -0400
Processing by Users::RegistrationsController#create as HTML
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"awN2GU8EYEfisU0",
"user"=>
{"profile_attributes"=>
{"first_name"=>"Dave",
"birthday(2i)"=>"4",
"birthday(3i)"=>"21",
"birthday(1i)"=>"1933",
"occupation_title"=>"Software Developer"},
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"email"=>"[email protected]"}}
Unpermitted parameters: profile_attributes
我已經安裝:
- 導軌4.0.0beta1,紅寶石2.0.0-P0
- 設計( 'rails4' 分支),Mongoid(從GIT)
- 自定義設計註冊控制器爲強參數添加定義。
模型/ user.rb:
class User
include Mongoid::Document
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :confirmable, :lockable, :timeoutable
field :email, type: String, default: ''
...
has_one :profile
accepts_nested_attributes_for :profile
end
模型/ profile.rb:
class Profile
include Mongoid::Document
include Mongoid::Timestamps
# Attributes
# ----------
field :slug, type: String, default: '' # Acts as user-'friendlier' slug
field :birthday, type: DateTime, default: DateTime.now
field :first_name, type: String, default: ''
field :occupation_title, type: String, default: ''
belongs_to :user
embeds_many :photos
has_one :occupation_industry, :as => :industry
end
控制器/用戶/ registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def resource_params
params.require(:user).permit(:email, :password, :password_confirmation, :profile_attributes)
end
private :resource_params
end
的routes.rb
devise_for :users,
:path => '',
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:sign_up => 'register'
},
:controllers => {
:registrations => "users/registrations",
:passwords => "users/passwords"
}
我已經看過這些相關的職位,他們似乎並沒有幫助:
編輯:
看起來設計並實際支持其「rails4」分支強參數(這是應該在幾天內被合併到主。)通過代碼看,你可以覆蓋設計控制器上每個動作的params函數。對於創建新用戶,在我的示例中,它的sign_up_params
而不是resource_params
。
儘管改變這個名字到正確的一個,它仍然沒有工作...只有白名單使用此爆炸的所有參數似乎工作:
def sign_up_params
params.require(:user).permit!
end
顯然,這種失敗的強烈目的參數...所以現在的問題是我如何允許我的嵌套屬性profile_attributes
(在我的原始問題中看到)?
你在哪裏把這個代碼重寫sign_up_params? – 2013-05-12 12:34:00
在從** Devise :: RegistrationsController **繼承的類中,這是你如何覆蓋/擴展它 – 2013-06-12 19:13:45
是的,這是做它的方式...對於哈希(像我的'profile_attributes'),你需要指定每個嵌入式屬性都由設計明確。即:'params.require(:user).permit(:email,:password,:password_confirmation,profile_attributes:[:first_name,:occupation_title])' – 2013-09-07 16:37:07