0

私の問題は、「clients」という名前のコントローラーで更新アクションを処理しようとすると、406 が受け入れられないことです (HTML を要求していて、JSON を処理していません)。これは、以下にリストされているネストされたプロセスのいずれかで検証が失敗した場合にのみ発生するためです。私はコードを調べて、いくつかの方法でリファクタリングを試みましたが、これを回避できないようです。新鮮な目が私が見ていないものを見ることを願っています.

def update
@client = Client.find(params[:id])



respond_to do |format|
if params[:save_contact]
    format.html { redirect_to "/clients/#{@client.id}/edit#tabs-3", :notice => 'Contact Saved!' }
    format.mobile { render :action => "edit", :notice => 'Contact Saved' }
    format.json { head :ok }
else
end
if params[:save_job]
    format.html { redirect_to "/clients/#{@client.id}/edit#tabs-6", :notice => 'Job Saved!' }
    format.mobile { render :action => "edit", :notice => 'Job Saved' }
    format.json { head :ok }

else
end




if @client.update_attributes(params[:client])
 @client.update_attribute(:branch_number, @client.branch.number)         

    if @client.wcrequested? && !@client.wcrequest_sent?
          @client.update_attribute(:wcstatus, "Pending")
          @client.update_attribute(:wcrequest_sent, "TRUE")
          @client.update_attribute(:wcresponse_sent, "FALSE")
          @client.update_attribute(:wcresponded, "FALSE")
          ClientsMailer.wcreq_recieved_corp(@client, current_user).deliver
          ClientsMailer.wcreq_recieved_branch(@client, current_user).deliver
    else
      format.html { render :action => "edit" }
      format.mobile { render :action => "edit" }
      format.json { render :json => @client.errors, :status => :unprocessable_entity }


    end

    if @client.wcstatus == "Denied"
           @client.update_attribute(:wcrequest_sent, "FALSE")
           @client.update_attribute(:wcrequested, "FALSE")
           ClientsMailer.wcreq_completed_corp(@client, current_user).deliver
           ClientsMailer.wcreq_completed_branch(@client, current_user).deliver
    else
    end

    if @client.wcresponded? && !@client.wcresponse_sent?
          @client.update_attribute(:wcresponse_sent, "TRUE")
          ClientsMailer.wcreq_completed_corp(@client, current_user).deliver
          ClientsMailer.wcreq_completed_branch(@client, current_user).deliver
    else
    end

    if params[:gpreq]
          @client.update_attribute(:gpstatus, "Pending GP Approval")
          ClientsMailer.gpreq_recieved_corp(@client, current_user).deliver
          ClientsMailer.gpreq_recieved_branch(@client, current_user).deliver
    else
    end
    if params[:gpreply]
          @client.update_attribute(:gpstatus, "GP Approval Completed")
          ClientsMailer.gpreq_completed_corp(@client, current_user).deliver
          ClientsMailer.gpreq_completed_branch(@client, current_user).deliver
   else
   end

    if @client.cred_requested? && !@client.cred_req_sent?
          @client.update_attribute(:cred_req_sent, "TRUE")
          ClientsMailer.credreq_recieved_corp(@client, current_user).deliver
          ClientsMailer.credreq_recieved_branch(@client, current_user).deliver
   else
   end
    if @client.cred_status == "Completed" && !@client.cred_rep_sent?
          @client.update_attribute(:cred_rep_sent, "TRUE")
          ClientsMailer.credreq_completed_corp(@client, current_user).deliver
          ClientsMailer.credreq_completed_branch(@client, current_user).deliver
  else
  end



      format.html { redirect_to edit_client_path(@client), :notice => 'Client was successfully updated!' }
      format.mobile { render :action => "edit", :notice => 'Client was successfully updated!' }
      format.json { head :ok }
      format.xml { render :action => "edit"}

else

      format.html { render :action => "edit" }
      format.mobile { render :action => "edit" }
      format.json { render :json => @client.errors, :status => :unprocessable_entity }
      format.xml { render :action => "edit"}


end
end
   end
4

2 に答える 2

0

わかったと思います。formatparamは他のものとは異なります。railsはAcceptヘッダーとparam:formatを使用して、html、xml、jsonなどで応答するかどうかを決定します。したがって、使用しようとしているすべての形式がサポートされているわけではないので、おそらくmobile1つが原因です。

于 2012-09-25T17:30:03.800 に答える
0

406 エラーは、関連する形式が利用できないか、対応していないことが原因で発生します。

まず、アクションを 1 つの Respond_to ブロックに絞り込み、ビジネス ロジックをモデルに移すことから始めることをお勧めします。次のようなもの@client.update_attribute(:wcstatus, "Pending")は、コントローラではなく、検証中のモデルにあるべきであり、save メソッドを呼び出すだけです。

if else end ステートメントが満たされていない条件があると思われます。ところで、else の直後に end が続くのは、単純に間違っています。

とにかく、それを切り詰めて、それがyuを残すところを見てから、条件を使用してローカル変数を設定し、リダイレクトにローカル変数を使用するrespond_toブロックを1つだけ保持して問題が発生するまで、条件を1つずつ追加します等...

于 2012-09-26T12:16:15.370 に答える