Jyothi Kumar
Jyothi Kumar

Reputation: 43

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.

Upvotes: 0

Views: 253

Answers (4)

kajal ojha
kajal ojha

Reputation: 1278

Say suppose you have a main page named home

In your home.html.erb write

<%= link_to "cars" cars_path %>
<%= link_to "users" users_path %>

Upvotes: 2

dnsh
dnsh

Reputation: 3633

You need to understand concept of routes in rails.

Run command rake routes in your project folder. You will see mapping like this.

          Prefix Verb   URI Pattern                            Controller#Action
       companies GET    /companies(.:format)                   companies#index

So according to routes, following link will lead to index action of companies controller.

<%= link_to 'Companies', companies_path%>

As you can observe, in companies_path, companies is prefix shown in routes.

For your specific case, following links will work. In your home.html.erb

<%=link_to "All Cars", cars_path%>
<%=link_to "All Users", users_path%>

Remember there is comma between "All Users" and users_path. Because link_to is just a method with multiple arguments.

For more info,

http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

Upvotes: 0

Akshay Borade
Akshay Borade

Reputation: 2472

Try this .......

<%=link_to "cars" cars_path(car) %>

<%=link_to "users" users_path(user) %>

This link will send you to show method of the controller (users/cars). Then you need to create a show page in your views/cars/show.html.erb and views/users/show.html.erb then you can display their own view/show pages.

Hope this will work for you.

Upvotes: 0

CoolDrinE
CoolDrinE

Reputation: 21

You probably should use rails scaffold to have a look at the functional pages generated by it at first.

Upvotes: 0

Related Questions