2016-08-20 77 views
0

我從用戶收到我的網站的報告,他們得到錯誤422當使用POST訪問「結果」頁面時。我不能重新創建這個錯誤,所以我想知道是否有任何在我的代碼下面會導致格式錯誤?我預計可能會出現錯誤,因爲我已將Rails 3.x項目升級到Rails 4.2。我在Rails(可能)中渲染導致422錯誤

我想知道是否有什麼明顯的代碼會產生422錯誤,或者如果有無論如何排除422錯誤。

基本上,在#show中有一個POST方法結果。它會創建一個結果文本並登錄到像/ this-post-name/result?這樣的url?r = abc123。我正在渲染#show in/result,因爲它基本上是再次加載相同的頁面,但帶有「結果框」。不得不使用/結果是我作爲新手程序員做出的選擇,並不是絕對必要的,我想。

我很確定錯誤在於「respond_to」,但無法弄清楚或解決它(即重新創建它)。

另外,我不確定這是否重要,但是我在此頁面上收到了大量的AuthencityToken錯誤。

編輯:我設法通過我的iPhone訪問它併發布表單,然後我禁用了Cookie並再次發送表單。這不會是人們會經常做的事情,但我想禁用Cookie可能會導致這種情況?

def show  
     @avaliable_posts = Post.where(:available => true) 
    end 


    def result  
    if request.get? && params[:r].blank? # Just visiting /result withoutout POST or ?r url 
     redirect_to category_path(@category)  
    else 
     set_round(session[:round_by_number])   

     # Either the visitor just posted the result or is revisiting through URL  
     if !params[:r].blank? # Visitor arrived from URL 
     @result = Result.find_by_scrambled_identifier(params[:r]) 
     params_to_use = @result.params_used 
     @params_to_use = @result.params_used 
     else 
     params_to_use = params 
     @params_to_use = params_to_use   
     end 

     post_instance = @post.get_post_instance(params_to_use)  

     if post_instance.valid?  

     @post_result_array = post_instance.calculate_me(params_to_use) 
     @post_result_text_array = @post_result_array[0] 


     respond_to do |format|   
      format.html { render :action => "show" } 
      format.json { render :json => @post } 
     end 

     else # post not valid 
     @errors = post_instance.errors 

     respond_to do |format|   
      format.html { render :action => "show" } 
      format.xml { render :xml => @validator.errors, :status => :unprocessable_entity } 
      format.json { render :json => @post } 
     end 
     end 
    end  
    end 

回答

1

422表示不可處理的實體。在你的代碼示例是唯一一個與此HTTP狀態代碼的地方:

format.xml { render :xml => @validator.errors, :status => :unprocessable_entity } 

顯然,當格式是XML和@validator包含一個錯誤發生這種情況。

編輯:

隨着對日誌中的異常,似乎第二聯動計算器問題的新的信息將被相關型號爲已知Rails issue

+0

嗯,是的,這是真的,但我真的不明白爲什麼普通用戶最終會出現這個錯誤?爲什麼這個PUT請求的正常提交被解釋爲XML?在我的版本中,我刪除了這一行,但我無法確認這是否解決了422錯誤。 – Christoffer

+0

沒有查看代碼很難說。我會在'format.xml {Rails.logger.error「...#{params.inspect}內部加強日誌記錄和跟蹤所有參數...」} – slowjack2k

+0

實際上,我認爲這個問題與protect_from_forgery和mobiles有關Cookie被禁用。它可能與我有另一個問題有關:http://stackoverflow.com/questions/39055480/invalidauthenticitytoken-errors-in-mobile – Christoffer