oscar martinez
Joined
Activity
I was trying as well with this diferent approach with code similar to the code Rohan use. But its not working.
Any sugestion?
class OrdersController < ApplicationController
def create
@order = current_user.orders.create(order_params)
redirect_to user_orders_path
end
end
def balance
if @order.save
@user.balance_update!(balance: @user.balance + @order.price)
end
end
private
def order_params
params.require(:order).permit(:price, :user_id)
end
end
This related I have a question:
I am trying the amount of @order pass and update the @balance. In my models both are integers.
I am trying this from Orders_controller but doesnt work.
Do you have any sugestion?
Do you need to see more code?
Any comment is welcome!!
class OrdersController < ApplicationController
def create
@order = current_user.orders.create(order_params)
@user.balance = balance: (@user.balance += @order.price)
redirect_to user_orders_path
end
end
private
def order_params
params.require(:order).permit(:price, :user_id)
end
end
And dont forget that balance is an attribute in User model
Hi every one:
I am having similar challenges regarding the creation of a payment system with a type of credits (simple virtual money).
Regarding the balance I am using this method in the User model and is working fine. Just in the case Rohan has some issues with Casey sugestion.
before_validation :load_defaults
def load_defaults
if self.new_record?
self.balance = 1000
end
end