2011-05-20 61 views
0

我正在創建與基於Devise身份驗證的成員ID關聯的配置文件。創建操作不會插入 - 導軌3

創建配置文件時,這些值不會插入到數據庫中。

路徑文件。

resources :troopers do 
    resource :trooper_profile 
    end 

騎兵模型

has_one :trooper_profile 

騎兵剖面模型

belongs_to :trooper 

騎兵控制器#創建動作

def create 
@trooper = current_trooper 
@profile = @trooper.build_trooper_profile(params[:profile]) 
respond_to do |format| 
    if @profile.save 
    format.html { redirect_to(trooper_trooper_profile_path, :notice => 'Profile was successfully created.') } 
    else 
    format.html { render :action => "new" } 
    end 
end 

檔案表

<%= form_for @profile, :url => trooper_trooper_profile_path(@trooper) do |f| %> 
<%= f.label :first_name %><br /> 
<%= f.text_field :first_name %><br /><br /> 
<%= f.label :last_name %><br /> 
<%= f.text_field :last_name %><br /><br /> 
<p><%= submit_tag "Create Profile" %></p> 
<% end %> 

服務器輸出

Started POST "/troopers/1/trooper_profile" for 127.0.0.1 at 2011-05-20 13:04:01 +1000 
Processing by TrooperProfilesController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LhT6b4xu5bIJ5kwhS74L7dpaGbuR5BTdirh9AziD+Ew=", "trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"} 
Trooper Load (0.5ms) SELECT "troopers".* FROM "troopers" WHERE ("troopers"."id" = 1) LIMIT 1 
TrooperProfile Load (0.3ms) SELECT "trooper_profiles".* FROM "trooper_profiles" WHERE ("trooper_profiles".trooper_id = 1) LIMIT 1 
Rendered trooper_profiles/_form.html.erb (11.7ms) 
Rendered shared/_head.html.erb (1.6ms) 
Rendered shared/_menutop.html.erb (1.2ms) 
Rendered trooper_profiles/new.html.erb within layouts/application (18.5ms) 
Completed 200 OK in 109ms (Views: 22.5ms | ActiveRecord: 0.8ms) 

我有在另一個應用此相同的設置,不明白爲什麼這是行不通的。

回答

0

更改以下...

@profile = @trooper.build_trooper_profile(params[:trooper_profile]) 

它出現的參數傳遞是:trooper_profile

trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"} 

但是,你參考:簡介

+0

我並不總是理解所有的軌道,但你的答案意味着我會知道下一次。謝謝。 – 2011-05-20 05:09:54

+0

樂意幫忙。有一件事會干擾它的一點是將TrooperProfile模型更改爲配置文件。你已經注意到了冗餘(例如,trooper_trooper_profile_path,trooper.trooper_profile),而如果它是Profile,它就像一個Rails應用程序開始讀取。 (trooper.profile,trooper_profile_path等)。理解你比我更瞭解你的應用程序,並意識到這不是你的問題......用它應得的一粒鹽來吧...... – DonaldSowell 2011-05-20 12:43:46

0

如果它正在渲染new視圖,那麼配置文件沒有保存 - 也許是驗證錯誤? TrooperProfile是否需要姓氏(在你的例子中是空白的)?