Ruby-on-rails 是什么导致 Rails 3 中出现 422 不可处理实体错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5527570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:50:27  来源:igfitidea点击:

What causes a 422 Unprocessable Entity Error in Rails 3?

ruby-on-railsruby-on-rails-3

提问by marcamillion

This is the error message from the logfile:

这是来自日志文件的错误消息:

Started POST "/stages" for 127.0.0.1 at 2011-04-02 23:22:18 -0500
Processing by StagesController#create as JS
  Parameters: {"utf8"=>"?", "authenticity_token"=>"ob37MMciudHqAnNXFoeofWyVfLnrTxlHfncyDsZLpsI=", "stage"=>{"project_id"=>"3", "name"=>"First"}}

  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
#<User id: 1, email: "[email protected]", encrypted_password: "a$qUbNGm6lZ366jRiE0vK0gOpxbGXD5JmfqWmH1lfLlCEC...", password_salt: "a$qUbNGm6lZ366jRiE0vK0gO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 264, current_sign_in_at: "2011-04-03 04:12:24", last_sign_in_at: "2011-04-03 03:21:37", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", username: "test", f_name: "Test", l_name: "User", created_at: "2011-01-22 07:17:45", updated_at: "2011-04-03 04:12:24", invitation_token: nil, invitation_sent_at: nil, plan_id: 3, current_state: nil, confirmation_token: nil, confirmed_at: "2011-02-11 23:19:15", confirmation_sent_at: "2011-02-11 23:18:20">

  Role Load (0.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles".id = "assignments".role_id WHERE (("assignments".user_id = 1))
Completed 422 Unprocessable Entity in 302ms (Views: 0.2ms | ActiveRecord: 1.5ms)

Any ideas ?

有任何想法吗 ?

This is the new & create actions in stage:

这是阶段中的新建和创建操作:

def new
    @project = Project.new
    respond_with(@project)
  end

def create
    #@project = current_user.projects.create(params[:project])
    @project = current_user.projects.build(params[:project])
    #@project.current_user = current_user
    if @project.save
      respond_with(@project, :status => :created, :location => @project) do |format|
        flash.now[:notice] = 'Project was successfully created.'
        format.html { redirect_to(@project) }
        format.js   { render :partial => "projects/show", :locals => {:project => @project}, :layout => false, :status => :created }
      end
    else
      respond_with(@project.errors, :status => :unprocessable_entity) do |format|
          format.js   { render :json => @project.errors, :layout => false, :status => :unprocessable_entity }
          format.html { render :action => "new" }
      end
    end
  end

This is the form partial that creates the new stage:

这是创建新阶段的表单部分:

<% stage ||= Stage.new 
   new_stage = stage.new_record? %>
<%= form_for(stage, :html => { :class=>"ajax-form", :id => "stage-ajax-form"}, :remote => true, :disable_with => (new_stage ? "Adding..." : "Saving...")) do |f| %>
    <%= f.hidden_field :project_id %>
    <%#= f.hidden_field :client_id, :value => @project.client.id %>
    <div class="validation-error" style="display:none"></div>
    <label for="stage_name"><span class="icon stage-icon"> </span></label>
    <input type="text" class="name" size="20" name="stage[name]" id="stage_name" value="<%=  stage.name %>" >

    <%= f.submit(new_stage ? "Add Stage" : "Save", :class => "green awesome") %>
<% end %>

采纳答案by marcamillion

Turns out that it was this line that was causing this error:

原来是这一行导致了这个错误:

@project = current_user.projects.build(params[:project])

@project = current_user.projects.build(params[:project])

I replaced the buildwith createand all works now.

buildcreate和替换了现在所有的工作。

回答by Evgenia Manolova

The answer here is, that any error in you case will result in '422 Unprocessable Entity' when you're responding in JSON format. The reason is this line in your controller:

这里的答案是,当您以 JSON 格式响应时,您遇到的任何错误都会导致“422 Unprocessable Entity”。原因是您的控制器中的这一行:

format.js   { render :json => @project.errors, :layout => false, :status => :unprocessable_entity }

I.e. when the object has errors and you are responding in JSON format, you will always send the 422 status.

即当对象有错误并且您以 JSON 格式响应时,您将始终发送 422 状态。

What you actually need is to make a further investigation why would your object have errors. And that could be anything. For example: when not persisting the @project, it may have caused a validation error, etc..

您真正需要做的是进一步调查为什么您的对象会出错。那可以是任何东西。例如:当不持久化@project 时,可能会导致验证错误等。

In that case your question is irrelevant and the accepted answer is misleading.

在这种情况下,您的问题无关紧要,接受的答案具有误导性。

IMHO, you should either change the question, or update the answer.

恕我直言,您应该更改问题或更新答案。