Form to change variable for a calculation in the same view

Asked

Viewed 496 times

0

I’m starting with the Rails.

I have a view that receives information from the database and by means of a model methods do several calculations.

For example:

<% @profundidade = 0.5 %>

<% @prnt = 90 %>

<% @analises.each do |analise| %>
  <tr>
    <td><%= valor_duascasas(analise.nctotalcomarea * @profundidade * @prnt) %></td>
  </tr>
<% end %>

I want to create a form that changes this value of @profundidade and @prnt.

2 answers

1

You don’t do this in the view, but in the controller.

Take the 2 variables as parameter, perform the calculation and return the data ready for the view.

0

Just complementing the answer about parameters. Just send the two variables as a parameter in the url (for example http://localhost:3000/parse/? prnt=9&depth=0.5)

After that, you can access the variables both in the controller and in the view, by the params variable and set these default values if you don’t get any. For example:

<% @profundidade = params[:profundidade] || 0.5 %>

<% @prnt = params[:prnt] || 90 %>

PS: I also suggest to pass these variables to the controller and only access them in the view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.