导致Rails 3中422不可处理的实体错误的原因是什么?

时间:2011-04-03 04:29:38

标签: ruby-on-rails ruby-on-rails-3

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

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: "test@abc.com", encrypted_password: "$2a$10$qUbNGm6lZ366jRiE0vK0gOpxbGXD5JmfqWmH1lfLlCEC...", password_salt: "$2a$10$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)

有什么想法吗?

这是新的&amp;在阶段创建行动:

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

这是创建新阶段的形式部分:

<% 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 %>

2 个答案:

答案 0 :(得分:22)

原来是这条线导致了这个错误:

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

我将build替换为create,现在一切正常。

答案 1 :(得分:7)

这里的答案是,当你以JSON格式回复时,你的任何错误都会导致'422 Unprocessable Entity'。 原因是你的控制器中有这一行:

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

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

您实际需要的是进一步调查您的对象为什么会出错。这可能是任何事情。例如:当不持久化@project时,它可能导致验证错误等。

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

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