I’m new in rails and I could need some help. Been stuck in this for few days. I’ve searched the solution but don’t get it still. The problem is I want to save the outcome of the for loop into database (postgreSQL).
<% @amount = (params[:h].to_d * params[:k].to_d) %>
<% @percent = (params[:h].to_d * params[:k].to_d) / params[:p].to_d %>
<% for i in 0..params[:a].to_i do %>
<% @newAmount = ((@amount/(@percent)) + @amount) %>
<%= "#{i}" + " - #{@amount}" %><br />
<% @amount=@newAmount %>
<% end %>
Very good would be if I could save the form data also, I found out that form_tag ain’t very good to save data, but I don’t really know how to do this with form_for.
<%= form_tag ('/calc') do %>
A_n: <%= text_field_tag :a_n %><br />
H: <%= text_field_tag :h %><br />
K: <%= text_field_tag :k %><br />
P: <%= text_field_tag :p %><br />
A: <%= text_field_tag :a %><br />
<%= submit_tag 'Calc' %>
<% end %>
I am sorry if this is basic or really simple (which I’m sure it is) but I hope I can find some help how to do this properly.
Model:
class Calculation < ActiveRecord::Base
attr_accessible :a, :a_n, :h, :k, :p
end
Table:
create_table "calculation", :force => true do |t|
t.string "a_n"
t.decimal "h"
t.decimal "k"
t.decimal "p"
t.integer "a"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
This is what my form looks like, with form_for its really easy to make the validations so why not make them?
And the show view is:
This works perfectly! Thank you for your attention and for help!