Skip to content

Instantly share code, notes, and snippets.

@jrbruce
Created November 20, 2010 09:27
Show Gist options
  • Save jrbruce/707726 to your computer and use it in GitHub Desktop.
Save jrbruce/707726 to your computer and use it in GitHub Desktop.
# helpers/application_helper.rb
module ApplicationHelper
def setup_user(user)
user.build_account unless user.account
user
end
end
# app/models/account_owner.rb
class AccountOwner < ActiveRecord::Base
has_one :account, :validate => true
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :account_attributes
accepts_nested_attributes_for :account
end
# app/models/account.rb
class Account < ActiveRecord::Base
belongs_to :account_owner
validates_presence_of :name
attr_accessible :name
end
# Devise Registration Form (modified)
<h2>Sign up</h2>
<%= form_for(setup_user(resource), :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<%= f.fields_for :account do |af| %>
<p><%= af.label :name %><br />
<%= af.text_field :name %></p>
<% end %>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment