Cố gắng thêm một thuộc tính tùy chỉnh lồng nhau, Hồ sơ (một tài liệu Mongoid), để đưa ra tôi tài lớp. Khi biểu mẫu đăng ký Devise được gửi, nó sẽ tạo cả một đối tượng Người dùng và tương ứng Tiểu sử.Rails 4/lập mưu/MongoDB: "thông số Unpermitted" sử dụng thuộc tính tùy chỉnh và các thông số mạnh mẽ
Tôi muốn kết quả cuối cùng để trông giống như thế này trong MongoDB tôi:
User:
{
# Devise fields:
"email": "[email protected]",
...
# Custom field
"profile" : "<object_id>"
}
hồ sơ:
{
"first_name": "Dave",
....
}
Thật không may, tôi là người nhận ving điều này trong bảng điều khiển của tôi bất cứ khi nào tôi gửi đăng ký của tôi. Nó tạo thành công một User nhưng không tạo được Profile được liên kết.
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
tôi đã thiết lập:
- Rails 4.0.0beta1, Ruby 2.0.0-p0
- lập mưu (chi nhánh 'rails4'), Mongoid (từ git)
- Bộ điều khiển đăng ký Devise tùy chỉnh để thêm định nghĩa cho thông số mạnh.
mô hình/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
mô hình/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
controllers/người dùng/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"
}
tôi đã xem xét những bài viết liên quan, họ không có vẻ để giúp:
CHỈNH SỬA:
Có vẻ như Devise thực sự hỗ trợ các thông số mạnh trong nhánh 'rails4' của nó (được cho là hợp nhất thành chính trong vài ngày tới.) Nhìn qua mã, nó xuất hiện bạn có thể ghi đè lên một hàm params cho mỗi hành động trên bộ điều khiển phát sinh. Để tạo người dùng mới, số sign_up_params
thay vì resource_params
trong ví dụ của tôi.
Mặc dù thay đổi tên này để người thích hợp, nó vẫn không làm việc ... chỉ danh sách trắng tất cả các thông số sử dụng tiếng nổ này dường như làm việc:
def sign_up_params
params.require(:user).permit!
end
Rõ ràng, loại đánh bại mục đích của mạnh các tham số ... vì vậy bây giờ câu hỏi là làm thế nào để tôi cho phép các thuộc tính lồng nhau của tôi profile_attributes
(như đã thấy trong câu hỏi ban đầu của tôi)?
Nơi nào bạn đặt mã này để ghi đè sign_up_params? –
Trong lớp kế thừa từ ** Devise :: RegistrationsController **, đây là cách bạn ghi đè/mở rộng nó –
Vâng, đây là cách để làm điều đó ... cho băm (như 'profile_attributes') của tôi, bạn cần phải xác định mọi thuộc tính được nhúng một cách rõ ràng bởi thiết kế. nghĩa là nó trông giống như: 'params.require (: user) .permit (: email,: password,: password_confirmation, profile_attributes: [: first_name,: Occupation_title])' –