0

I have two models

User(id, name, is_host,....)

Host(id, name, user_id,...)

With the Association

User hasOne Host

I am using Devise for the User Registration. When a user wants to register as a Host i want to set is_host = 1 and create a row in Host with the name of the Host he provides in the Registration Form.

What i want to do?

  1. Allow Registration Form to accept Host details
  2. Create the association data when user selects register as host

Am trying to figure where i can write the logic based on what the user chooses to register as and then create the associated row in Host.

User From

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <% f.label :password_confirmation %><br />
    <% f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :is_host %><br />
    <%= radio_button("user", "is_host", true) %> Yes
    <br/>
    <%= radio_button("user", "is_host", false) %> No
  </div>

  <div class="actions">
    <%= f.submit "Signup" %>
  </div>
<% end %>
Community
  • 1
  • 1
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • It would be good if you post the form code – Rajdeep Singh Apr 21 '15 at 07:19
  • 1
    I think this can help you, overriding devise controllers: http://stackoverflow.com/questions/3546289/override-devise-registrations-controller or a better answer given by @RSB – Deepesh Apr 21 '15 at 07:40

1 Answers1

2

Simple way is to add an attr_accessor to the user model, e.g.

attr_accessor :host_name

And use this in the form as

= f.text_field :host_name

You can add an after_create callback in the model to save the host based upon the value of the radio button

after_create :create_user_host, if: Proc.new { |user| user.is_host }

def create_user_host
  self.create_host(name: host_name)  
end

or

You can use https://github.com/ryanb/nested_form gem to save the association directly. Just add

 accepts_nested_attributes_for :host

and in the form

= nested_form_for resource do |f|
  # code for user
  = f.fields_for :host do |host_f|
    = host_f.text_field :name

More details can be found in nested_form gem documentation.

Hope this helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • thanks Paji :) one more question. 'self.create_host(name: host_name) ' will this create a row in the 'hosts' table – Harsha M V Apr 21 '15 at 07:46
  • Yes, it will create a host associated with the user – Rajdeep Singh Apr 21 '15 at 07:47
  • But dont we need to add something like Host.create(name: host_name) – Harsha M V Apr 21 '15 at 07:50
  • No thats not required, `create_host` method is used because it has one to one relation with the user – Rajdeep Singh Apr 21 '15 at 07:51
  • Still if you want to create it using `create` than you can write this instead, `Host.create(name: host_name, user_id: self.id)` – Rajdeep Singh Apr 21 '15 at 07:52
  • wow. Rails magic :P one more thing. Is it possible for me to have two forms. One sign up for Users and One Sign up for Hosts. and submit to the same controller to let the above magic take charge? – Harsha M V Apr 21 '15 at 07:54
  • You can surely do that, in the migration set `is_host` default to `false`. If there's a form where `host` fields are not present then `after_create` callback will not be executed we have added a condition there. As you can see, to implement the functionality I have written everything in model. You don't have to change anything in controller side. – Rajdeep Singh Apr 21 '15 at 07:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75776/discussion-between-harsha-m-v-and-rsb). – Harsha M V Apr 21 '15 at 07:59