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