2009-05-20 79 views
0

我有一個Ruby on Rails頁面來編輯用戶配置文件。該表單正確顯示用戶名和電子郵件。當我在文本框中更改名稱並單擊更新時,我最終返回到edit_profile頁面,並顯示消息「配置文件已成功更新。」。問題是我將名稱更改爲未保存到數據庫的值。問題保存導軌形式

沒有錯誤,服務器輸出中的參數看起來正確。

處理UsersController#更新(在22點00分四十八秒2009-05-19 127.0.0.1)[PUT]參數: { 「用戶」=> { 「名稱」=> 「新名稱」 , 「email」=>「[email protected]」}, 「commit」=>「更新」, 「action」=>「update」,「_method」=>「put」, 「authenticity_token」= > 「59c79fa90aaf5558aaab8cddef6acb7a4c7c55c3」, 「ID」=> 「1」, 「控制器」=> 「用戶」}

我缺少什麼?

edit_profile.html.erb

<% form_for @profile, :url => {:action => "update", :id => @profile} do |f| %> 
    <%= f.error_messages %> 
    <p>Name: <%= f.text_field :name %></p> 
    <p>Email: <%= f.text_field :email %></p> 
    <%= f.submit "Update" %> 
<% end %> 

users_controller.rb

def edit_profile 
    @profile = User.find(current_user.id) 
    end 

    def update 
    @profile = User.find(params[:id]) 
    respond_to do |format| 
     if @profile.update_attributes(params[:profile]) 
      flash[:notice] = 'Profile was successfully updated.' 
      format.html { render :action => 'edit_profile' }   
     else 
      flash[:notice] = 'Profile Error.' 
      format.html { render :action => "edit_profile" }   
     end 
     end 
    end 

編輯: 是的,這是一個被命名的問題,要解決這個問題,我改變......

if @profile.update_attributes(params[:profile]) 

if @profile.update_attributes(params[:user]) 

回答

1

字段的名稱與傳遞給update_attributes方法的名稱不匹配。

檢查表單字段的名稱(使用Firebug),以及將「名」等

但是你逝去稱爲「PARAMS」的數組:

update_attributes(params[:profile]) 

你的表單字段應該被稱爲「配置文件[名稱]」,以使其正常工作。

如果您檢查development.log,您應該看到沒有更新被調用過。

+0

是的,你是完全正確的。謝謝託比。 – Mark 2009-05-20 02:32:45