Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Last active December 16, 2015 20:09
Show Gist options
  • Save telagraphic/5489824 to your computer and use it in GitHub Desktop.
Save telagraphic/5489824 to your computer and use it in GitHub Desktop.
ActiveRecord::RecordInvalid in PaymentsController#create
Validation failed: Amount is not a number
Rails.root: /Users/telagraphic/Documents/programming/rails_projects/life-ulator
Application Trace | Framework Trace | Full Trace
app/models/payment.rb:9:in `save_and_update'
app/controllers/payments_controller.rb:8:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"4pxlp8J/lb21CW2z7W7wuFVfnClOfGdtbARXstoUisE=",
"payment"=>{"amount"=>"asdf"},
"commit"=>"+payment",
"debt_id"=>"6"}
class Payment < ActiveRecord::Base
attr_accessible :amount
validates :amount, presence: true, :numericality => true
belongs_to :debt
def save_and_update(debt)
self.transaction do
self.save!
debt.update_attributes!(amount: debt.amount - self.amount)
end
end
end
def create
@debt = Debt.find params[:debt_id]
@payment = @debt.payments.build params[:payment]
@payment.user_id = current_user.id
if @payment.save_and_update(@debt)
redirect_to @debt
else
render @debt
end
end
<%= form_for [@debt, @payment] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :amount %><br>
<%= f.text_field :amount %><br>
<%= f.submit '+payment' %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment