jeudi 8 décembre 2016

Views in ruby on rails

I am learning to code in ruby on rails environment. I created two models cars and products. I want to have a main page which have a link to cars and products. On clicking each of them they should display their own view pages. Below are view pages for cars and users respectively :

app/views/cars/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Cars</h1>

<table>
  <thead>
    <tr>
      <th>Make</th>
      <th>Color</th>
      <th>Year</th>
      <th colspan="24"></th>
    </tr>
  </thead>

  <tbody>
    <% @cars.each do |car| %>
      <tr>
        <td><%= car.make %></td>
        <td><%= car.color %></td>
        <td><%= car.year %></td>
        <td><%= link_to 'Show', car %></td>
        <td><%= link_to 'Edit', edit_car_path(car) %></td>
        <td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Car', new_car_path %>

<h4>Import that data!</h4>
  <%= form_tag import_users_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import CSV" %>
  <% end %>
</div>

app/views/users/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Users</h1>

<table>
  <thead>
    <tr>
      <th>User</th>
      <th>Steps</th>
      <th>Distance</th>
      <th>Minutes Exercised</th>
      <th>Hours of Sleep</th>
      <th>Calories Burned</th>
      <th colspan="24"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.user %></td>
        <td><%= user.steps %></td>
        <td><%= user.distance %></td>
        <td><%= user.exercise %></td>
        <td><%= user.sleep %></td>
        <td><%= user.calories %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>
<div>

<h4>Import that data!</h4>
  <%= form_tag import_users_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import CSV" %>
  <% end %>
</div>

<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %>
  <div class="form-group">
    <%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %>
  </div>

<% end %>

I don't know how to create single main page and provide links to these views. One example would help me. Thanks in advance guys.

Aucun commentaire:

Enregistrer un commentaire