I've been stumped by this problem for several hours now and I'm hoping someone can help me figure out why this code isn't working.
What I'm trying to do is create an account associated with a particular user. I can create a user, but when I go to create the account, it doesn't look like the form is posting to the create method in my controller (the array of form data shows up appended to accounts/new in my browser bar).
Here are my models:
class User < ActiveRecord::Base
attr_accessible :email_address, :password, :password_confirmation
has_secure_password
has_one :account, dependent: :destroy
before_save {|user| user.email_address = email_address.downcase}
And my Account model:
class Account < ActiveRecord::Base
attr_accessible :cell_phone, :first_name, :home_phone, :last_name, :middle_initial, :work_phone
belongs_to :user
has_many :addresses
has_many :orders
has_many :items
#strip digits and return string of numbers
before_save do |account|
account.cell_phone = cell_phone.gsub(/\D/,'')
account.home_phone = home_phone.gsub(/\D/,'')
account.work_phone = work_phone.gsub(/\D/,'')
end
before_save do |account|
account.first_name = first_name.capitalize
account.last_name = last_name.capitalize
account.middle_initial = middle_initial.capitalize
end
In my User Controller (this works properly):
def new
if signed_in?
@user = current_user
redirect_to @user
else
@user = User.new
end
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
redirect_to new_account_path
else
flash[:error] = "Sorry, something went wrong"
render 'new'
end
end
And my Account Controller (where I feel like the problem is):
class AccountsController < ApplicationController
#before_filter :get_current_user
before_filter :signed_in_user
def new
@user = current_user
@account = @user.build_account(params[:account])
end
def create
@user = current_user
@account = @user.build_account(params[:account])
if @account.save
flash[:success]
redirect_to user_path(current_user)
else
flash[:error]
redirect_to root_path
end
end
I have current_user and sign_in as methods in a Helper file.
And, lastly, my Account form
<%= form_for(@account) do |form| %>
<%= render 'shared/user_form_error_messages', object: form.object %>
<%= form.label :first_name, "First Name" %>
<%= form.text_field :first_name %>
<%= form.label :middle_initial, "Middle Initial (optional)" %>
<%= form.text_field :middle_initial %>
<%= form.label :last_name, "Last Name" %>
<%= form.text_field :last_name %>
<%= form.label :home_phone, "Home Phone" %>
<%= form.text_field :home_phone %>
<%= form.label :cell_phone, "Cell Phone" %>
<%= form.text_field :cell_phone %>
<%= form.label :work_phone, "Work Phone" %>
<%= form.text_field :work_phone %>
<%= form.submit "Complete Account", class:"btn" %>
<% end %>
Thanks in advance for your help. I've been googling examples, reading rdocs and have tried a bunch of different things. If someone could explain why my code doesn't work (as opposed to just giving me the answer), I'd be most grateful.
At @thesis request, here is the output from my log when I click "submit" on the form:
Started GET "/accounts/new? utf8=%E2%9C%93&authenticity_token=DQ5mOiICI0C83l9xgkSlvToQKyWjE3Adm3X3U0HJ1W4%3D&account%5Bfirst_name%5D=Jesse&account%5Bmiddle_initial%5D=A&account%5Blast_name%5D=Flores&account%5Bhome_phone%5D=555-555-5555&account%5Bcell_phone%5D=&account%5Bwork_phone%5D=&commit=Complete+Account" for 127.0.0.1 at 2012-10-04 16:14:11 -0400
Connecting to database specified by database.yml
Processing by AccountsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DQ5mOiICI0C83l9xgkSlvToQKyWjE3Adm3X3U0HJ1W4=", "account"=>{"first_name"=>"Jesse", "middle_initial"=>"A", "last_name"=>"Flores", "home_phone"=>"555-555-5555", "cell_phone"=>"", "work_phone"=>""}, "commit"=>"Complete Account"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'B_E1bsUASQhHC-Zb-6updg' LIMIT 1
Account Load (0.6ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = 2 LIMIT 1
(0.1ms) begin transaction
(0.0ms) commit transaction
Rendered shared/_user_form_error_messages.html.erb (0.5ms)
Rendered forms/_form_account.html.erb (5.9ms)
Rendered accounts/new.html.erb within layouts/application (13.2ms)
Rendered layouts/_navigation.html.erb (0.4ms)
Rendered forms/_form_header_signin.html.erb (0.8ms)
Rendered layouts/_header.html.erb (2.7ms)
Rendered layouts/_messages.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 279ms (Views: 160.1ms | ActiveRecord: 4.6ms)