You're getting confused with how Rails works
- Rails is server-side
- You're trying to calculate client-side
- You have to process them separately
Modularity
You'll have to calculate this on the back-end (once you've submitted the form)
Rails views are there to show an HTML page, and won't change unless a request is performed to the server
In keeping with Rail's modular structure, you need to keep your logic in your controller, your inputs in your views, and your data manipulation in your models. Therefore, you only need to pass the different Candy's to your controller, allowing it to sum the total:
#app/views/candys/new.html.erb
<%= form_for(@candy) do |f| %>
<%= f.label :candy, 'Add a candy' %>
<%= f.number_field :candy%>
<%= f.submit %>
<% end %>
#app/controllers/candys_controller.rb
def new
@candy = Candy.new
end
def create
@candy = Candy.new(candy_params)
@candy.save
@total_candy = #your logic here
end
private
def candy_params
params.require(:candy).permit(:candy)
end
class=""? You can just omit theclassattribute if you don't have a value for it.