2015-03-20 29 views
5

Ví dụ: khi tôi truy cập users/invitations/new, trường duy nhất là :email. Tôi muốn mời một người dùng và, ngoài việc cung cấp email của họ, cung cấp:Làm cách nào để mời người dùng (sử dụng devise_invitable) và điền các trường bổ sung trong quá trình mời?

  • first_name
  • last_name
  • vai trò
  • công ty (user belongs_to company)

Tôi tạo ra Users::InvitationsController < Devise::InvitationsController :

class Users::InvitationsController < Devise::InvitationsController 
    private 
    def resource_params 
    params.permit(user: [:email, :invitation_token, :role, :company_id])[:user] 
    end 
end 

và tôi đã thêm các trường đó vào users/invitations/new. Lời mời gửi tốt, nhưng khi tôi chấp nhận và nhập mật khẩu, xác nhận của tôi không thành công nói rằng No role is selected (b/c xác thực).

Làm cách nào để tôi có thể đặt các trường này trước khi gửi lời mời và yêu cầu chúng tồn tại và lưu khi lời mời được chấp nhận? Cảm ơn!

+0

HI. bạn có tìm ra giải pháp cho việc này không? im chạy vào cùng một vấn đề. thanks – loloso

+0

Không, xin lỗi. Tôi đã kết thúc việc họ đặt sau trong quá trình – jackerman09

Trả lời

1

Rails 5

Đây là giải pháp của tôi sử dụng accepts_nested_attributes_for. Nếu thuộc tính tùy chỉnh của bạn trực tiếp trên mô hình người dùng, bạn sẽ có thể thay thế profile_attributes: [:first_name, :last_name] bằng :first_name, :last_name, :role, :company.

Đây là bộ điều khiển của tôi.

class InvitationsController < Devise::InvitationsController 
    before_action :update_sanitized_params, only: :update 

    # PUT /resource/invitation 
    def update 
    respond_to do |format| 
     format.js do 
     invitation_token = Devise.token_generator.digest(resource_class, :invitation_token, update_resource_params[:invitation_token]) 
     self.resource = resource_class.where(invitation_token: invitation_token).first 
     resource.skip_password = true 
     resource.update_attributes update_resource_params.except(:invitation_token) 
     end 
     format.html do 
     super 
     end 
    end 
    end 


    protected 

    def update_sanitized_params 
    devise_parameter_sanitizer.permit(:accept_invitation, keys: [:password, :password_confirmation, :invitation_token, profile_attributes: [:first_name, :last_name]]) 
    end 
end 

Bên trong hình thức của tôi

<%= f.fields_for :profile do |p| %> 
    <div class="form-group"> 
     <%= p.label :first_name, class: 'sr-only' %> 
     <%= p.text_field :first_name, autofocus: true, class: 'form-control', placeholder: 'First name' %> 
    </div> 

    <div class="form-group"> 
     <%= p.label :last_name, class: 'sr-only' %> 
     <%= p.text_field :last_name, class: 'form-control', placeholder: 'Last name' %> 
    </div> 
    <% end %> 

Trong user.rb Tôi có

... 
accepts_nested_attributes_for :profile, reject_if: proc { |attributes| attributes[:first_name].blank? } 
Các vấn đề liên quan