user.rb

1
2
3
4
5
6
7
class User < ActiveRecord::Base
  attr_accessible :id, :name, :zipcode

  has_one :authentication, :inverse_of => :user, :dependent => :destroy
  accepts_nested_attributes_for :authentication
end

authentication.rb

1
2
3
4
5
6
7
class Authentication < ActiveRecord::Base
  attr_accessible :id, :user_id, :email

  has_one :authenticationdetail
  belongs_to :user
end

authenticationdetail.rb

1
2
3
4
class Authenticationdetail < ActiveRecord::Base
  belongs_to :authentication
end

authentications_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
class AuthenticationsController < ApplicationController
  def new 
    @user ||= User.find(params[:user_id] if params[:user_id]
    if @user
      @authentication = @user.build_authentication(:user_id => @user.id)
    else
      redirect_to root_path
      flash[:notice] = "Error occured"
    end
  end
end

authentication form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%= form_for [@user, @authentication] do |a| %>
  <% @user.errors.full_messages.each do |msg| %>
    <p><%= msg %></p>
  <% end %>
  <%= a.fields_for :authentication do |builder| %>
    <div>
      <p>
        <%= builder.label :email %>
        <%= builder.text_field :email %>
      </p>
      <p>
        <%= builder.label :email_confirmation %>
        <%= builder.text_field :email_confirmation %>
      </p>
    </div>
  <% end %>
  <p class="submit">
    <%= a.submit "continue" %>
  </p>
<% end %>