2012-09-25 34 views
0

我的問題是,當我試圖在名爲「clients」的控制器中處理更新操作時(我正在請​​求HTML並且未處理JSON),我得到的406不可接受,我的預感是我在我的嵌套if語句的結構中缺少一些東西,因爲這隻發生在下面列出的嵌套進程之一的驗證失敗時。我已經通過我的代碼,並試圖重構幾種方式,但似乎無法繞過這一個。希望一組新的眼睛可能會看到我沒有看到的東西。RoR返回406不可接受,請求HTML

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? && [email protected]_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? && [email protected]_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? && [email protected]_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" && [email protected]_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 
+0

我重新編寫了可讀性代碼。請注意,正如@ksol所說,末尾有兩個「末尾」與任何內容都不相符。我假設他們是複製/粘貼剩餘物。 – numbers1311407

+0

你可以發佈你的堆棧跟蹤 – jamesc

回答

0

我想我明白了。 format參數不像其他類型:rails使用Accept Headers和:format param來確定是否使用html,xml,json或其他不同的方式響應。所以我會說你沒有支持你嘗試使用的所有格式,可能原因之一是mobile

+0

哎呀謝謝,是的,這是控制器行動的結束 –

+0

這解釋了一個'末端',而不是另一個 – ksol

+0

一個是爲控制器另一個是爲respond_to do | format | –

0

406錯誤是由相關格式不可用或照顧造成的。

我建議你先將操作縮減爲一個respond_to塊,然後將你的業務邏輯轉移到你的模型中。 像@client.update_attribute(:wcstatus, "Pending")這樣的事情不應該在控制器中,而是在驗證模型下,然後調用save方法。

我懷疑你有一個條件,如果你的其他結束語句沒有滿足你的條件。順便說一句,緊接着就是結束了,這顯然是錯誤的。

無論如何,先把它剪下來,然後看看那裏留下的是什麼,然後逐個添加這些註釋,直到問題出現,注意使用這些條件來設置局部變量並只保留一個使用用於重定向等的局部變量...