1

在我的用戶模型更新工作完全沒問題...... 無論我在配置文件模型中做什麼,控制器視圖我的數據庫都不會更新。在我的用戶模型中,我遇到了同樣的問題,但發現這是因爲我需要密碼字段。但是在這裏,我不這樣做,因爲我沒有在配置文件模型中設置任何驗證。紅寶石軌道不會更新配置文件模型的屬性

我不知道發生了什麼事。終端中的日誌顯示正在找到current_user.id,並且我正在使用該結果在我的配置文件表中查找user_id,該工作正常。更新不會發生。

什麼可能是錯的?

親切的問候

日誌

--- &id001 !ruby/object:Profile 
_already_called: 
    ? 
    - :autosave_associated_records_for_user 
    - :user 
    : false 

_start_transaction_state: 
    :id: 1 
    :new_record: false 
    :destroyed: false 
    :level: 1 
aggregation_cache: {} 

association_cache: {} 

attributes: 
    id: 1 
    user_id: 1 
    motd: Success is guaranteed! 
    first_name: Foo 
    last_name: Bar 
    birthday: 
    star_sign: 
    gender: 
    marital_status: 
    sexual_preference: 
    racial_background: 
    location: 
    profile_url: 
    about_me: 
    height: 
    body_type: 
    eye_colour: 
    drugs: 
    alcohol: 
    cigarettes: 
    likes: 
    dislikes: 
    bad_habits: 
    food: 
    music: 
    television: 
    book: 
    animal: 
    place: 
    possesion: 
    profile_visits: 
    created_at: 2012-01-07 23:02:29.228500 
    updated_at: 2012-01-07 23:02:29.228500 
attributes_cache: 
    birthday: 
    created_at: 2012-01-07 23:02:29.228500 Z 
    updated_at: 2012-01-07 23:02:29.228500 Z 
changed_attributes: {} 

destroyed: false 
errors: !ruby/object:ActiveModel::Errors 
    base: *id001 
    messages: !omap 
    - :motd: [] 

    - :first_name: [] 

    - :last_name: [] 

marked_for_destruction: false 
new_record: false 
previously_changed: !map:ActiveSupport::HashWithIndifferentAccess {} 

readonly: false 
relation: 
validation_context: 
Profile updated 
--- !map:ActiveSupport::HashWithIndifferentAccess 
utf8: "\xE2\x9C\x93" 
_method: put 
authenticity_token: YZwwOx5CqfKTfsybXE4NH2o2dTg4asKZZu6QOR9Y3Zo= 
profile: !map:ActiveSupport::HashWithIndifferentAccess 
    motd: Success is guaranteed! 
    first_name: Foo 
    last_name: Bar 
commit: update 
action: update 
controller: profiles 
id: "1" 

路線

resources :users 
    resources :sessions 
    resources :passwords 
    resources :profiles 


    root :to     => "users#new" 
    match 'success'   => "users#success" 
    match 'login'    => "sessions#new" 
    match 'logout'    => "sessions#destroy" 
    match 'reset_password'  => "passwords#new" 
    match 'setup_new_password' => "passwords#edit" 
    match 'settings', :to  => "users#settings" 


    match "/settings/account", :to => "users#account" 
    match "/settings/edit_profile", :to => "profiles#edit_profile" 


    match '/:username', :controller => 'users', :action => 'show' 

型號

class Profile < ActiveRecord::Base 

    belongs_to :user 

    attr_accessible :first_name, :last_name, :motd 


end 

Profiles_controller

def new 
    @user = Profile.new 
    end 

    def update 
     @user = Profile.find_by_user_id(current_user.id) 
     if @user.update_attributes(params[:user]) 
     render 'edit_profile' 
     flash[:success] = "Profile updated" 
     else 
     redirect_to root_path 
     end 
     end 

    def edit 

    end 

    def edit_profile 
    @user = Profile.find_by_user_id(current_user.id) 
    end 

end 

查看

<div id="formo"> <%= form_for @user, do |f| %> 

<%= f.text_field :motd, :placeholder => "motd" %><br /> 
<%= f.text_field :first_name, :placeholder => "fname" %><br /> 
<%= f.text_field :last_name, :placeholder => "lname" %><br /> 

<%= f.submit 'update' %><br /> 
<% end %> 
<%= debug(@user) %> 
<div class="notify"> 
    <% flash.each do |name, msg| %> 
      <%= content_tag :div, msg, :class => "#{name}" %> 
    <% end %> 
</div> 

</div> 

回答

2

變化:

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

要:

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

你也可能需要在該實例變量@profile重命名,所以你就不會感到困惑。

+0

這實際上傷害了!所有這些時間都是這樣。所以基本上,參數中的符號是指所討論的模型?謝謝。 – LondonGuy 2012-01-08 18:56:01

+0

是的,params散列中的鍵名來自您所引用的對象的類名稱。表單構建器是設置的。 '<%= form_for @user,do | f | %>'< - 該行 – ctcherry 2012-01-08 18:58:25

+0

@GregoryHouse始終打印您的參數。 '<%= params%>'在你的視圖或你的控制器中。當你在你的視圖中打印時,你會知道散列,它的值和keys.You然後可以在瀏覽器中檢查它。當控制器時,請檢查你的'/ log/development.log'或你的服務器以獲取信息。這會給你一個關於什麼是參數的詳細信息,你需要明確什麼。 – ktkaushik 2012-01-09 01:14:50