dimanche 13 juin 2021

How to display a calculation in Ruby?

I am trying to display a simple calculation for how much gas one may need for a trip. I have my logic in my view for now so that I can easily display the value. Here is the form the user fills out:

    <h1>New Trip!</h1>
        <br>
        <br>
        <%= f.label :title %>
        <%= f.text_field :title %>
        <br>
        <br>
        <%= f.label :content %>
        <%= f.text_area :content %>
        <br>
        <br>
        <%= f.label :mileage %>
        <%= f.number_field :mileage, step: :any %>
        <br>
        <br>
        <%= f.label :mpg %>
        <%= f.number_field :mpg, step: :any %>

        <br>
        <br>
        <%= f.label :amount %>
        <%= f.number_field :amount, step: :any %>

        <%= f.submit %>
    <% end %>
<% end %>

I would like to have the value of this calculation displayed next to the roadtrip they created. Here is what I have in my partial view that displays the user's trips:

 <ul>
        <% roadtrips.each do |r| %>
            <li><%= link_to r.title, roadtrip_path(r) %> - <%= r.name%> </li>%
           <p><%= (r.mileage / r.mpg)*2.90 %></p>
        <% end %>
    </ul>

When I go to create the trip and click submit I do get a nil error on '/' not sure why because I thought that's how you divide in Ruby.

Any feedback/suggestions would be greatly appreciated. Thanks in advanced!

Aucun commentaire:

Enregistrer un commentaire