2014-03-12 54 views
0

我有一個form_for使用AJAX來更新我添加到設計用戶模型的自定義字段。但是,在提交(有效輸入)後,我得到一個POST 422(不可處理實體)錯誤。這是因爲低於我的用戶模型驗證的:在設計模型AJAX調用中的Rails4 422(Unprocessable Entity)

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

更新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 
+0

所以驗證只在這個動作來看,我已經包括current_user.enable_strict_validation = true,所以除去這將是一個問題。 –

+0

我要求刪除current_user.save!而不是current_user.enable_strict_validation = true# –

+0

由於您尚未在current_user中設置要更新的值(例如'annual_income','current_savings'等),所以'current_user .save!'必然會導致驗證錯誤。當你執行'current_user.update_attributes(user_params)'時,你傳遞'user_params',所以你不會得到任何驗證錯誤。 –