Rails4 422(Unprocessable Entity)在设计模型AJAX调用中

时间:2014-03-12 16:21:56

标签: ruby-on-rails ajax devise

我有一个form_for,它使用AJAX来更新我添加到设计用户模型的自定义字段。但是,在提交(有效输入)后,我收到POST 422(不可处理的实体)错误。这是因为我的用户模型中的验证:

class User < ActiveRecord::Base
  validates_presence_of :annual_income, :current_savings, :retirement_savings, :if => :enable_strict_validation
  validates_numericality_of :annual_income, :current_savings, :retirement_savings, :if => :enable_strict_validation

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable
end

以下是表格:

<div class="main_form">
  <h1>Income & Savings First...</h1>
  <%= form_for @user, url: { action: :persist_data }, method: :post, remote: true, html: { id: "insurance_form" } do |f| %>
    <%= f.label "Total Annual Income" %>
    <%= f.text_field :annual_income, autofocus: true, placeholder: "Enter $" ,value: "", class: "format_me" %><br/>
    <%= f.label "Savings" %>
    <%= f.text_field :current_savings, placeholder: "Enter $", value: "", class: "format_me" %><br/>
    <%= f.label "Saved for Retirement?" %>
    <%= f.text_field :retirement_savings, placeholder: "Enter $",  value: "", class: "format_me" %><br/>
    <%= f.submit "Calculate", class: "btn btn-primary" %>
  <% end %>
</div>

调用以下HomeController:

class HomeController < ApplicationController
  before_filter :authenticate_user!

  def index
    @user = current_user
  end

  # TODO: refactor as update in a custom devise controller for user
  def persist_data
    user_params.each {|key, val| val.gsub!("$", "") }
    # Make sure user model validations are run
    current_user.enable_strict_validation = true
    current_user.save!
    current_user.update_attributes( user_params )
    render json: {status: "OK"}
  end

  private

  def user_params
    params.require(:user).permit(:annual_income, :current_savings, :retirement_savings, :recommended_insurance)
  end
end

最后,这是带有错误的服务器日志:

Started POST "/home/persist_data" for 127.0.0.1 at 2014-03-12 12:11:34 -0400
Processing by HomeController#persist_data as JS
  Parameters: {"utf8"=>"✓", "user"=>{"annual_income"=>"$4.00", "current_savings"=>"$4.00", "retirement_savings"=>"$4.00"}, "commit"=>"Calculate"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 35ms

ActiveRecord::RecordInvalid (Validation failed: Annual income can't be blank, Annual income is not a number, Current savings can't be blank, Current savings is not a number, Retirement savings can't be blank, Retirement savings is not a number):
  app/controllers/home_controller.rb:13:in `persist_data'

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

更新persist_data,如下所示:

  def persist_data
    ## Set the user_params value
    user_params = user_params.each {|key, val| val.gsub!("$", "") } 
    # Make sure user model validations are run
    current_user.enable_strict_validation = true
    ## Remove below line, as you are updating in the very next line with user_params
    ## current_user.save!           
    current_user.update_attributes( user_params ) 
    render json: {status: "OK"}
  end