1

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

Community
  • 1
  • 1
do_Ob
  • 709
  • 1
  • 6
  • 24

2 Answers2

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.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Thank you for this. How about on edit? can I use `employee_params` also? where I will declare `current_password` field? Because I tried to save it, but it is not working. – do_Ob Oct 06 '15 at 07:24
0

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

everyman
  • 3,377
  • 1
  • 34
  • 33
  • how about using `build_resource(params[employee][:user][:email], params[employee][:user][:password])` but how I will use this if my controller is like this `class EmployeesController < ApplicationController` – do_Ob Oct 02 '15 at 07:29