i_trope
i_trope

Reputation: 1604

Rails4 422 (Unprocessable Entity) in devise model AJAX call

I have a form_for that uses AJAX to update custom fields I added to the devise user model. However, upon submit (with valid input), I get a POST 422 (Unprocessable Entity) error. This occurs because of the validations in my user model below:

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

Here is the form:

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

The call is made to the HomeController below:

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

And lastly, here is the server log with the error:

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'

Thanks for the help.

Upvotes: 0

Views: 2687

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53038

Update persist_data as below:

  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

Upvotes: 1

Related Questions