Created
January 3, 2011 23:12
-
-
Save safarista/764151 to your computer and use it in GitHub Desktop.
Creating users with a profile that has an address association all at once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Sign up</h2> | |
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> | |
<%= devise_error_messages! %> | |
<p><%= f.label :email %><br /> | |
<%= f.email_field :email, :required => true %></p> | |
<p><%= f.label :password %><br /> | |
<%= f.password_field :password, :required => true %></p> | |
<p><%= f.label :password_confirmation %><br /> | |
<%= f.password_field :password_confirmation, :required => true %></p> | |
<%= f.fields_for :profile do |profile_fields| %><br /> | |
<p><i>This is required</i><%= profile_fields.text_field :name, :required => true %></p> | |
<% end %> | |
<p><%= f.submit "Sign up" %></p> | |
<% end %> | |
<%= render :partial => "devise/shared/links" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########User Models######### | |
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, :lockable and :timeoutable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable; :omniauthable | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :email, :password, :password_confirmation, :remember_me | |
has_one :profile, :dependent => :destroy | |
accepts_nested_attributes_for :profile, :allow_destroy => true | |
end | |
#############Profile Model#### | |
class Profile < ActiveRecord::Base | |
belongs_to :user | |
has_one :address, :dependent => :destroy | |
accepts_nested_attributes_for :address, :allow_destroy => true | |
has_attached_file :avatar, :styles => {:medium => "300x600>", :thumb => "221x272>"} | |
has_many :photos | |
end | |
########Address Model############ | |
class Address < ActiveRecord::Base | |
belongs_to :profile | |
end | |
######CONTROLLERS######### | |
class RegistrationsController < Devise::RegistrationsController | |
before_filter :authenticate_user! | |
def new | |
super | |
#@user = User.new | |
@user.profiles.build.addresses.build | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
# POST /users | |
# POST /users.xml | |
def create | |
@user = User.new(params[:user]) | |
respond_to do |format| | |
if @user.save | |
format.html { redirect_to(@user) } | |
format.xml { render :xml => @user, :status => :created, :location => @user } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @user.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
###SCHEMA | |
create_table "addresses", :force => true do |t| | |
t.string "city" | |
t.string "region" | |
t.string "postcode" | |
t.string "country" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.integer "profile_id" | |
end | |
create_table "profiles", :force => true do |t| | |
t.string "name" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.integer "user_id" | |
end | |
#QUESTION | |
I want to auto create a profile for every user that signs_up. The profile has an address. The user is created but not the profile and address. i think am failing to pass some fields with the builder. How do i do that? | |
#Registration View | |
<h2>Sign up</h2> | |
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> | |
<%= devise_error_messages! %> | |
<p><%= f.label :email %><br /> | |
<%= f.email_field :email, :required => true %></p> | |
<p><%= f.label :password %><br /> | |
<%= f.password_field :password, :required => true %></p> | |
<p><%= f.label :password_confirmation %><br /> | |
<%= f.password_field :password_confirmation, :required => true %></p> | |
<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