1
我在更新具有嵌套屬性的對象時遇到了一些問題。在rails中更新嵌套屬性時的問題
我的模型對象:
用戶
class User < ActiveRecord::Base
has_one :portal_user
accepts_nested_attributes_for :portal_user
validates_presence_of :username
end
PortalUser
class PortalUser < ActiveRecord::Base
belongs_to :user,:dependent => :destroy, :foreign_key => :user_id
end
我有定義的操作update
在用戶控制器是這樣的:
class UsersController < ApplicationController
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
flash[:error] = "Error while updating personnal information."
format.html { render :action => edit_profiles_path }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
end
用於PortalUser控制器更新動作也被定義:
class PortalUsersController < ApplicationController
def update
@portal_user = PortalUser.find(params[:id])
respond_to do |format|
if @portal_user.update_attributes(params[:portal_user])
format.html { redirect_to(@portal_user, :notice => 'PortalUser was successfully updated.') }
format.xml { head :ok }
else
flash[:error] = "Error while updating PortalUser."
format.html { render :action => edit_profiles_path }
format.xml { render :xml => @portal_user.errors, :status => :unprocessable_entity }
end
end
end
end
最後我有一個Profile
控制器承載嵌套形式:
class ProfilesController < ApplicationController
def edit
# Note: I am using a hard coded Id on purpose.
@user = User.find(980190962)
end
end
查看個人資料的操作edit
:
<%= form_for @user do |f| %>
<%= f.fields_for :portal_user do |f_portal_user| %>
<%= f_portal_user.label :firstname %> <br/>
<%= f_portal_user.text_field :firstname %> <br/>
<%= f_portal_user.label :lastname %> <br/>
<%= f_portal_user.text_field :lastname %> <br/>
<% end %>
<%= f.label :username %> <br/>
<%= f.text_field :username %><br/>
<%= f.fields_for :portal_user do |f_portal_user| %>
<%= f_portal_user.label :phone %> <br/>
<%= f_portal_user.text_field :phone %><br/>
<%= f_portal_user.label :cellular_phone %> <br/>
<%= f_portal_user.text_field :cellular_phone %><br/>
<% end %>
<%= submit_tag "Update" %>
<% end %>
當我進入編輯頁面時,我可以看到加載到表單中的用戶信息(包括@user和@portal_user),但是當我編輯表單併發送更新時,什麼也沒有發生!
爲了幫助你發現我的問題,這裏的原點是「追蹤」(在此我試圖重新命名爲用戶名字段從改變Amokrane到AMK:
Started POST "https://stackoverflow.com/users/980190962" for 127.0.0.1 at 2011-02-13 19:38:05 +0100
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UA+dbbmwZKpbNYscIvEsqPFwlBkr7yEok1xpYP3/T6k=", "user"=>{"portal_user_attributes"=>{"firstname"=>"Amokrane", "lastname"=>"Chentir", "id"=>"980190962", "phone"=>"", "cellular_phone"=>"0668002010"}, "username"=>"amk"}, "commit"=>"Update", "id"=>"980190962"}
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 980190962) LIMIT 1
SQL (0.1ms) BEGIN
PortalUser Load (0.3ms) SELECT `portal_users`.* FROM `portal_users` WHERE (`portal_users`.user_id = 980190962) LIMIT 1
SQL (0.2ms) ROLLBACK
SQL (0.1ms) BEGIN
SQL (0.1ms) ROLLBACK
DEPRECATION WARNING: Giving a path to render :action is deprecated. Please use render :template instead. (called from realtime at /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/benchmark.rb:309)
Rendered profiles/edit.html.erb within layouts/application (15.1ms)
Completed 200 OK in 215ms (Views: 23.5ms | ActiveRecord: 0.9ms)
這可能是很明顯的東西給我是比較新的軌道!
謝謝!