The Rails’ Way of doing things is to have fat models and thin views. In other ways, most of the code that deals with critical operations like calculations, database manipulations need to be done at a “model” level. Remember that Rails is a Ruby framework, where as Ruby is object oriented. And by being an object-oriented programming language, most of the operations happen within the object itself.
If you want to get the calculations automatically, consider using AJAX.
@radhames. I see that the link (Screencast) you sent is done mostly on
the console.
What I'm actually asking about is how to define calculation methods that
are used to calculate a specific formula from some fields, and the
result will be the value for another field.
Is there a tutorial or example relating this issue?
Are you looking to make this happen in the browser, say, in JavaScript? Or are you trying to do this within Rails, say using a form submit or an Ajax submit to send the values to the server and get the value back into the page with RJS?
Are you looking to make this happen in the browser, say, in
JavaScript? Or are you trying to do this within Rails, say using a
form submit or an Ajax submit to send the values to the server and get
the value back into the page with RJS?
the thing is you can add methods to the models and they become available as if they were attributes, they are virtual attributes so oyu can have in your product model.
def tax
price * tax_percent/100
end
and then you can do
@product = Product.find (params[:id])
@product.tax
and @product .tax you return the something like US$15.00