Creating employee with devise user

狂风中的少年 提交于 2019-12-23 03:44:26

问题


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 ^_^


回答1:


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.




回答2:


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...



来源:https://stackoverflow.com/questions/32900140/creating-employee-with-devise-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!