0

I have received reports from users to my website that they get Error 422 when visiting a "result" page using POST. I cannot re-create this error at all so I am wondering if there is anything in my code below that would cause this error in formatting? I expect there could be errors here since I have upgraded a Rails 3.x project to a Rails 4.2.

I would either like to know if there is anything obvious in the code that would create 422 errors or if there is anyway to troubleshoot 422-errors.

Basically, in #show there is a POST method to result. It is creating a result text and lands on a url like /this-post-name/result?r=abc123 . I am rendering #show in /result because it is basically loading the same page again but with a "result box". Having to use /result is a choice I made as a newbie programmer and is not absolutely necessary, I think.

I am quite sure the error lies within the "respond_to" but can't figure that out, or troubleshoot it (i.e. re-create it).

Also, I am not sure if this is important, but I get tons of AuthencityToken errors on this page.

Edit: I managed to recreate this issue by accessing it through my iPhone and post a form, then I disabled cookies and send the form again. That would not be something people would do often but I guess having cookies disabled may cause this?

  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
Christoffer
  • 2,271
  • 3
  • 26
  • 57

2 Answers2

1

A 422 means Unprocessable Entity. Within your sample code is only one place with this http status code:

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

Obviously this happens when format is XML and @validator contains an error.

Edit:

With the new information about the exception within the logs and the second linked stackoverflow question it seems to be releated to a known Rails issue

slowjack2k
  • 2,566
  • 1
  • 15
  • 23
  • Well, yes, that is true but I can't really see why a regular user would end up with this error? Why would a normal submission of this PUT request be interpreted as XML? In my version I have removed this line but I cannot confirm if this is solvin the 422 error or not. – Christoffer Aug 20 '16 at 10:47
  • It's difficult to say without view code. I would enhance logging and trace all parameters inside `format.xml { Rails.logger.error "... #{params.inspect}..." }` – slowjack2k Aug 20 '16 at 12:08
  • Actually, I believe this issue has to do with protect_from_forgery and mobiles with cookies disabled. It is likely connected with another issue I have: http://stackoverflow.com/questions/39055480/invalidauthenticitytoken-errors-in-mobile – Christoffer Aug 20 '16 at 15:11
0

It seems like this issue is related to another issue that I have written another question for. I have an InvalidAuthencityToken issue with my website and the exceptions created through that cause a 422 (and not a 500) error as far as I understand from http://api.rubyonrails.org/v2.3/classes/ActionController/RequestForgeryProtection/ClassMethods.html

I am not 100% sure that this is the same issue but it seems quite likely and therefore I will close this question.

Community
  • 1
  • 1
Christoffer
  • 2,271
  • 3
  • 26
  • 57