Tôi đang làm việc trên một ứng dụng web bằng Devise and Rails 4. Tôi có một mô hình Người dùng mà tôi đã mở rộng với 2 trường biểu mẫu bổ sung sao cho khi người dùng đăng ký, anh ấy cũng có thể gửi/họ. (dựa trên http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/). Bây giờ tôi muốn thêm một mô hình Tổ chức giáo dục. Mô hình này has_many: người dùng và người dùng thuộc về: cơ quan. Tôi muốn có thể đăng ký tên tổ chức tên trên cùng một biểu mẫu tôi đăng ký người dùng. Tôi biết tôi cần một nested_attribute trong mô hình Institution của mình, vì đây là phụ huynh, mà tôi sẽ hiển thị trong một chút. Khi tôi cố gắng đăng ký người dùng tôi nhận được trong bảng điều khiển: Thông số không được xác minh: Các tổ chức.Rails 4.0 với Devise. Thuộc tính lồng nhau Các tham số không được chấp nhận
Gợi ý của tôi là tôi không thể cập nhật lớp cha mẹ (Tổ chức) dựa trên lớp con của tôi (Người dùng). Có thể có một giải pháp cho điều này? Hay có ai có kinh nghiệm gì đó tương tự?
class Institutions < ActiveRecord::Base
has_many :users,
accepts_nested_attributes_for :users
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :institution
end
đăng ký/new.html.erb Ở đây tôi có các hình thức lồng nhau
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
.
.
<%= f.fields_for :institutions do |i| %>
<p><%= i.label :name %><br />
<%= i.text_field :institutions_attr %></p>
<% end %>
Dựa trên hướng dẫn, tôi đã liên kết trước đó, tôi đã tạo ra một tài khoản mới :: ParameterSanitizer mà kế thừa từ Devise :: ParameterSanitizer và ghi đè phương thức sign_up
như sau:
lib/user_sanitizer.rb
private
def sign_up
default_params.permit(:first_name, :last_name ,:email, :password, :password_confirmation, :current_password, institutions_attributes: [:id, :name])
end
Cuối cùng, application_controller.rb tôi
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
protected
def devise_parameter_sanitizer
if resource_class == User
User::ParameterSanitizer.new(User, :user, params)
else
super
end
end
end
Cảm ơn bạn đã đọc!
điều khiển params đầu ra:
{"utf8"=>"✓",
"authenticity_token"=>"JKuN6K5l0iwFsj/25B7GKDj7WEHR4DO3oaVyGxGJKvU=",
"user"=>{"email"=>"[email protected]",
"first_name"=>"abc",
"last_name"=>"xyz",
"institutions"=>{"name"=>"Government"},
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
EDIT
Như đã đề cập, tôi đã thêm
params.require(resource_name).permit(:email, :first_name, :last_name, institution: [:name], :password, :password_confirmation) and I get an *error syntax error, unexpected ',', expecting => ...nstitution: [:name], :password, :password_confirmation)*
NHƯNG, nếu tôi chỉnh sửa lại để
params.require(resource_name).permit(:email, :first_name, :last_name, :password, :password_confirmation, institution: [:name])
Tôi không nhận được lỗi cú pháp nhưng tôi nhận được thông số không được phép: Các tổ chức trong Yêu cầu.
Niềm tin của tôi là điều này xảy ra vì Người dùng là con của Học viện. Tôi có, tuy nhiên, không thể tìm thấy một công việc xung quanh này.
Câu trả lời tuyệt vời –
Cảm ơn nhiều King'ori, đây là chính xác những gì tôi đang tìm kiếm!Đã tiết kiệm cho tôi thêm một vài giờ thất vọng. – DerProgrammer
@ king'ori Maina Tôi đang sử dụng đá quý devise_auth_token và sau khi rơi trên phương pháp cho cùng một gem Tôi vẫn nhận được tham số Unpermitted: lỗi tài khoản trong nhật ký máy chủ của tôi – Anjan