简体   繁体   中英

Creating employee with devise user

It looks like this Profile model for Devise users? but mine is different.

I want to create employee record and this employee have an option to have a user account or not. I am using devise gem for my user.

So I have Employee model with

has_one :user
accepts_nested_attributes_for :user

and User model

belongs_to :employee

I know that I need to put nested user in my view, But how to save it in controller?

It is not something like this, right?

params.require(:employee).permit(:name, user_attributes: [:email, :password])

Can you give some hint or some useful link I can follow? It is hard to modify the devise. Thank you ^_^

Devise shouldn't be modified; it's just a series of controllers and a model.

If you want to create an employee , and then pass data through to the User model, you've taken the right approach.

What you'll need to do is keep to convention (IE build the User model etc) to ensure the functionality of Devise is maintained:


#app/controllers/employees_controller.rb
class EmployeesController < ApplicationController
   def new
      @employee = Employee.new
      @employee.build_user
   end

   def create
      @employee = Employee.new employee_params
      @employee.save
   end

   private

   def employee_params
      params.require(:employee).permit(:name, user_attributes: [:email, :password, :password_confirmation])
   end
end

Now, the only problem with this is that user_attributes may be false, as Devise is famously finicky with its strong params.

--

To get the form to work, you'll be able to use the following:

#app/views/employees/new.html.erb
<%= form_for @employee do |f| %>
   <%= f.text_field :name %>
   <%= f.fields_for :user do |user| %>
      <%= user.email_field :email %>
      <%= user.password_field :password %>
      <%= user.password_field :password_confirmation %>
   <% end %>
   <%= f.submit %>
<% end %>

I don't have any reason - or tests - to disprove this. Devise even suggests that you can load forms using the User.new invocation. Thus, I'd surmise that the main hurdle you'll have is the passing of the strong params.

Hope I didn't misunderstood, but no need to modify Devise. Just pass all params you need and if valid, create the user directly:

params.require(:employee).permit(:name, 
                                 :user_wants_account, 
                                 :whatever, 
                                 :user => [:email, :password, :password_confirmation])

# manually
User.create(:email => params[:employee][:user][:email], 
            :password => params[:employee][:user][:password], 
            :password_confirmation => params[:employee][:user][:password_confirmation],
            :employee_id => self)

If you want to use the "default" login forms - no problem - since each user has an employee relation...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM