2010-12-01 4 views
0

現在のソリューションでは、2つの異なる変数名を持つ両方の値をビューからコントローラに渡し、次にコントローラのロジックを使用して、どちらを使用して更新を行うかを決定します。それは機能しますが、よりよい方法が必要だと私は考えています。何かアドバイス?選択リストまたは同じフォーム(Rails 3)のテキストフィールドから同じ値を更新するにはどうすればよいですか?

===ビュー===

<p>Choose tutor from list:&nbsp;<%= f.collection_select(:current_tutor, @tutors, 
:name, :name, {:include_blank => true}) %></p> 
<p>..or dd new tutor:&nbsp;<%= f.text_field :current_tutor_textfield %></p> 

===コントローラ===

respond_to do |format| 
    @student = Student.where(:slug => params[:id]).first 

    # Here I'm deciding which value will be passed to the update as the new_tutor 
unless params[:student][:current_tutor].blank? 
     new_tutor = params[:student][:current_tutor] 
    end 
    unless params[:student][:current_tutor_textfield].blank? 
     new_tutor = params[:student][:current_tutor_textfield] 
    end 

if @student.update_tutor(new_tutor) 
     format.html { redirect_to(students_path, 
       :notice => 'Student was successfully updated.') } 
     format.xml { head :ok } 
    else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @post.errors, 
       :status => :unprocessable_entity } 
    end 
end 

答えて

0

私は「けれども、私は、それを行うためのより良い方法の多くを知りませんおそらく、あなたがチューターと一緒に行くチューターを決めている場所のビットをきれいにするでしょう。これらの2つのステートメントは、相互に排他的です。新しいチューター名が既存のチューターの選択を無効にすることを意図している場合を除いて、両方のチュートリアルを実行できます。

+0

これは... "新しいチューターの名前は、既存のチューターの選択を上書きします" ...まさに何が起こるのですか? –

関連する問題