Harsha M V
Harsha M V

Reputation: 54949

Rails Devise allow Custom Fields in the Signup Form

I have two types of registration on on my website.

  1. User
  2. Host

There are two models related to Registration

users(id, name, is_host, ...)

hosts(company_name, user_id, status, ...)

Every Host is a User by default on the application. When the User signups on the website he has to enter the following fields

  1. Name
  2. Email
  3. Password
  4. Phone Number

When a Host Signups he has to enter the following

  1. Name
  2. Email
  3. Password
  4. Phone Number
  5. Company Name

On submitting the form as a host it should save the data in the User model and also set the is_host flag to 1 which other wise is 0 and then store the company_name in the hosts model.

What i have done?

I have installed devise and generated the migration tables and generated the scoped views.

What i am trying to achieve?

  1. Add custom fields in the signup form, How to set the strong parameter for the User sign up form?
  2. how to have a separate signup form for the same model and act as a registration for HOSTS

views/users/registrations/new.html.erb

<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 :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="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "users/shared/links" %>

Server Error:

Unpermitted parameters: first_name, last_name

Upvotes: 3

Views: 1441

Answers (1)

shikha
shikha

Reputation: 669

Please look this example....

we create the migration – nothing special here

class AddFieldsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end

app/views/devise/registrations/new.html.slim

h2 Sign up

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

    div
     = f.label :first_name
    br
     = f.text_field :first_name, autofocus: true

    div
     = f.label :last_name
    br
     = f.text_field :last_name

    div
     = f.label :email
    br
     = f.email_field :email

    div
     = f.label :password
    br
     = f.password_field :password

    div
     = f.label :password_confirmation
    br
     = f.password_field :password_confirmation

    div
     = f.submit 'Sign up'

= render 'devise/shared/links'

Customizing the RegistrationsController

def sign_up_params
  devise_parameter_sanitizer.sanitize(:sign_up)
end

def account_update_params
  devise_parameter_sanitizer.sanitize(:account_update)
end

registrations controller

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params

    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)

  end

  def account_update_params

    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)

  end
end

config/routes.rb

devise_for :users, :controllers => { registrations: 'registrations' }

Upvotes: 3

Related Questions