중첩 된 속성이있는 객체를 업데이트 할 때 몇 가지 문제가 있습니다.레일에서 중첩 된 속성을 업데이트 할 때의 문제
내 모델 객체는 다음과 같습니다
사용자 내가 정의 작업 update
을 가지고
class PortalUser < ActiveRecord::Base
belongs_to :user,:dependent => :destroy, :foreign_key => :user_id
end
class User < ActiveRecord::Base
has_one :portal_user
accepts_nested_attributes_for :portal_user
validates_presence_of :username
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 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 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)의 정보를 볼 수 있지만 양식을 편집하고 업데이트를 보내면 아무 일도 일어나지 않습니다!
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)
그것은 수 :
여기 내 문제의 근원을 발견 할 수 있도록하기 위해 내가 AMK에 Amokrane에서 사용자 이름 필드를 변경하기 위해 이름을 변경하려고이의 '추적'(이다 분명 뭔가 내가 레일 비교적 새로운 오전 주어진!
감사합니다!이