0

I created Devise Users table and Type table. I added the type_id column to the users table through migration.

Below are the links in home.html.erb:

<%= link_to 'Basic Sign up', new_user_registration_path(type: @basic_type), class: 'btn btn-success'%>
<%= link_to 'Pro Sign up', new_user_registration_path(type: @pro_type), class: 'button' %>

Below is the pages_controller.rb

class PagesController < ApplicationController
    def home
        @basic_type = Type.find(1)
        @pro_type = Type.find(2)
    end

    def about
    end
end

Below are the models:

type.rb

class Type < ActiveRecord::Base
  has_many :users
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  belongs_to :type
 end

This is my migration file of type:

class CreateTypes < ActiveRecord::Migration
  def change
    create_table :types do |t|
        t.string :name

        t.timestamps
    end
  end
end

This is the migration file adding types to user:

class AddTypeToUser < ActiveRecord::Migration
  def change
    add_column :users, :type_id, :integer
  end
end

Users table in schema.rb:

 create_table "users", force: :cascade do |t|
    t.string   "email",                  limit: 255, default: "", null: false
    t.string   "encrypted_password",     limit: 255, default: "", null: false
    t.string   "reset_password_token",   limit: 255
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          limit: 4,   default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip",     limit: 255
    t.string   "last_sign_in_ip",        limit: 255
    t.datetime "created_at",                                      null: false
    t.datetime "updated_at",                                      null: false
    t.integer  "type_id",                limit: 4
  end

These are my registration forms: _basic_form.html.erb:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {id: 'basic_type'}) do |f| %>
    <%= devise_error_messages! %>

    <%= hidden_field_tag 'type', params[:type] %>

    <div class="field form-group">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true, class: 'form-control' %>
    </div>

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

    <div class="field form-group">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
    </div>

    <div class="actions form-group">
        <%= f.submit "Sign up", class: 'btn btn-success' %>
    </div>
<% end %>

_pro_form.html.erb:

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

    <%= hidden_field_tag 'type', params[:type] %>

    <div class="field form-group">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true, class: 'form-control' %>
    </div>

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

    <div class="field form-group">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
    </div>

    <div class="actions form-group">
        <%= f.submit "Sign up", class: 'btn btn-success', id: 'form-submit-btn' %>
    </div>
<% end %>

new.html.erb:

<div class="row">
  <div class="col-md-6 col-md-offset-3 text-center">
    <% if params[:type] == '2'%>
      <h1>Pro Account</h1>
      <p>Sign up for the pro account!</p>
    <% else %>
      <h1>Basic Account</h1>
      <p>Sign up for free and get basic access to our community.</p>
    <% end %>
  </div>
  <div class="col-md-6 col-md-offset-3">
    <div class="well">
      <h2>Sign up</h2>

        <% if params[:type] == '2'%>
          <%= render "pro_form"%>
        <% else %>
          <%= render "basic_form"%>
        <% end %>

        <div class="actions form-group btn btn-default">
          <%= render "devise/shared/links" %>
        </div>
      </div>
  </div>
</div>

When I am signing up as basic or as pro in URL it is showing:

http://localhost:3000/users/sign_up?type=1

OR

http://localhost:3000/users/sign_up?type=2

But in database in the type_id column it is showing as nil.

0 Answers0