I'm getting the error "wrong number of arguments (2 for 0) from 'build_profile' function in user.rb while trying to add a profile to a user using the one to one relationship.
Models
User.rb
class User < ActiveRecored::Base
has_one :profile, dependent: :destroy
after_create :build_profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password, :password_confirmation, :profile_attributes
def build_profile
Profile.create(user: self)
end
end
Profile.rb
class Profile < ActiveRecord::Base
attr_accessible :name. :address, :age
belongs_to :user
validates :name, presence: true, length: { maximum: 50 }
validates :address, :age, presence: true
end
Users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
@profile = self.build_profile
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "welcome, Thanks for Signing up"
redirect_to @user
else
render 'new'
end
end
end
Profiles_controller.rb
class ProfilesController < ApplicationController
def edit
@profile = Profile.find(params[:id])
end
def show
@user.profile = User.find(params[:id]).profile
end
def destroy
end
end
I get this error when a user tries to sign up , below is the sign up form
Sign Up Form
<h1> Sign Up </h1>
<div class="span6 offset3">
<%= form_for (setup_user(@user)) do |f| %>
<%= f.fields_for :profile do |ff| %>
<p>
<%= ff.label :name %>
<%= ff.text_field :name %>
</p>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.pasword_field :password %>
</p>
<p>
<%= f.label :password_conformation, "Confirm Password" %>
<%= f.password_field :password_confirmation%>
</p>
<%= f.submit "create my account", class: "btn btn-large btn-primary" %>
<% end %>
Thanks
Server Log Output for posting the user new form
Started POST "/users" for 127.0.0.1 at 2014-10-13 10:01:42 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"G£ô", "authenticity_token"=>"7PQwoH4VGDE2kyHqjkv4PegFz/A
KYGYXRFtQpn9UKko=", "user"=>{"profile_attributes"=>{"name"=>"Example Test"}, "em
ail"=>"example@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[F
ILTERED]"}, "commit"=>"Create my account"}
(0.0ms) BEGIN
User Exists (25.0ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'ex
ample@test.com' LIMIT 1
(0.0ms) ROLLBACK
Rendered users/new.html.erb within layouts/application (4.0ms)
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`remember_token
` IS NULL LIMIT 1
Rendered layouts/_header.html.erb (3.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 330ms (Views: 93.0ms | ActiveRecord: 26.0ms)