2012-10-04 55 views
1

我已經好幾個小時難住了這個問題,現在,我希望有人能幫助我弄清楚爲什麼這個代碼不工作。麻煩與Rails的HAS_ONE協會

我試圖做的是建立與特定用戶相關聯的賬戶。我可以創建一個用戶,但是當我去創建帳戶時,它看起來不像表單發佈到我的控制器中的create方法(表單數據的數組在我的瀏覽器欄中顯示爲附加到帳戶/新) 。

這裏是我的模型:

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} 

我的賬戶模型:

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 

在我的用戶控制器(這工作正常):

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 

我的賬戶控制器(我覺得問題是這樣的):

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 

我有CURRENT_USER和sign_in作爲一個輔助文件的方法。

而且,最後,我的賬戶形式提前對您有所幫助

<%= 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 %> 

感謝。我一直在使用谷歌搜索的例子,閱讀rdocs,並嘗試了一堆不同的東西。如果有人能解釋爲什麼我的代碼不起作用(而不是僅僅給我答案),我會非常感激。

在@thesis要求,這裏是從我的日誌輸出,當我點擊「提交」的形式:

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) 
+0

請更新您的問題,並在表格發佈過程中添加日誌部分。 – thesis

+0

@thesis這是你的意思嗎? – jflores

回答

0

對不起,我第一次被誤解的問題。但是,您似乎希望將現有的user對象與新的account關聯起來。這是一個可以幫助你的鏈接。

https://stackoverflow.com/a/11335976/1160106

大問題聲明,但簡明扼要的回答。讓我知道它是否對你有幫助。

+0

感謝您的建議。然而,它沒有奏效。即使我按照這些說明修改了路由路徑(即,使用嵌套在用戶下的單一資源),我得到以下錯誤:未定義的方法'user_accounts_path'爲#<#:0x007fd9b4de5338>。 – jflores

+0

不應該是'user_account_path'而不是'user_accounts_path'?注意'accounts'是'account'。 「耙路」展示了什麼? – Samiron

+0

感謝Samiron,這幫了很大的忙!我還假設當一個表單被提交時,它會自動發佈到我的創建方法。所以,我將build_account和.save方法放在我的'new'方法中,並且只是從那裏處理帳戶創建。 – jflores

0

希望這將幫助你在你的代碼嵌套的建設情況。 在accountscontroller,

def new 
    @user = current_user 
    @account = @user.accounts.build(params[:account]) 
    end 

    def create 
    @user = current_user 
    @account = @user.accounts.build(params[:account]) 
    if @account.save 
     flash[:success] 
+0

嗨shanmuk,謝謝你的建議。不幸的是,這是行不通的。因爲它是一個has_one關係,所以我沒有一個Active Record對象來使用構建或創建方法。根據Rails文檔,我必須使用build_association或create_association方法來創建關聯的記錄。 – jflores