0

私はhas_one :preference Userモデルを持っているが、私は彼の好みを更新するために、ユーザーとフォームを更新するために、フォームに参加したいと思いますので、ユーザーの形で、私が追加:のRails:変更のユーザ嗜好フォーム

<% if [email protected]_record? %> 
    <div class="field"> 
    <%= label_tag 'user_preference_quote_type', 'Type de citations' %><br /> 
    <%= text_field_tag 'user_preference_quote_type', @user.preference.quote_type %> 
    </div> 
    <div class="field"> 
    <%= label_tag 'user_preference_locale', 'Langage' %><br /> 
    <%= text_field_tag 'user_preference_locale', @user.preference.locale %> 
    </div> 
<% end %> 

そして、私のコントローラで:

def update 
    @user = User.find(params[:id]) 
    @user.preference.quote_type = params[:user_preference_quote_type] 
    @user.preference.locale = params[:user_preference_locale] 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to(@user, :notice => t('c.users.update')) } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @user.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

しかし好みは、私はそれを行うことができますどのように、変更されないでしょうか?それを行うにはより良い方法がありますか?

答えて

2

これは良い方法ではありません。 fields_forを使用してください。あなたのフォームをきれいにします。ちなみに、update_attributesを使用して、あなたのアサイメントが役に立たなくなるモデルを更新するため、コントローラで何をしているのかは機能していません。上記のfields_forを使用すると、コントローラのクリーニングにも役立ちます。あなたのモデルには必ずaccepts_nested_attributes_forが必要なので注意してください。

+0

は、私はRailsの中に、このための美しいものがあることを確認しました、ありがとう、しかし:(それを見つけることができませんでした。 – Dorian

関連する問題