mardi 12 avril 2016

Rails - nested model inside devise model

I have two models which is Members and Company... Members is the devise model... When I sign up I want to include the following fields in the signup form.

  1. Name
  2. Email address
  3. Password
  4. Company name (from Company model)
  5. company type (from Company model)

Member has one company

I am trying to create the signup form via nested form.. But I am not sure to build the form for the Company to receive input from the user...

Here is my controller

class Brands::Members::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    @company = Company.new
    super
  end

  # POST /resource
  def create
    @company = Company.new(configure_sign_up_params)
    @company.valid?

    super
  end
end

Here is my View

<h2>Sign up</h2>

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

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

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

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <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>
  -->
<%= @company.errors %>
  <%= fields_for @company do |fc| %>
    <div class="field">
      <%= fc.label :name %><br />
      <%= fc.text_field :name %>
    </div>

  <% end %>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "brands/members/shared/links" %>

Aucun commentaire:

Enregistrer un commentaire