how do I link pages on rails?

14,454

Solution 1

Please look into http://guides.rubyonrails.org/routing.html - it is a great reference! Depending on what the link target is supposed to be you are probably looking for:

<%= link_to 'User (view)', user_path(@user) %>
<%= link_to 'User (edit)', edit_user_path(@user) %>
<%= link_to 'User (index)', users_path %>

Best, Ben.

Solution 2

In your view you want to use an appropriate path helper for each. To get the index pages:

<%= link_to 'users', users_path %>
<%= link_to 'microposts', microposts_path %>
Share:
14,454
Marshall Lanners
Author by

Marshall Lanners

Updated on June 04, 2022

Comments

  • Marshall Lanners
    Marshall Lanners almost 2 years

    I have three resources for my demo_app that I made. I made a home page but I wanted to put links of my home page to link to my three resources. I put out the code

    <%= link_to 'users', @user %> | 
    <%= link_to 'microposts', @micropost %> |
    <%= link_to 'task', @task %> |
    

    on my home page and the links show up on the page but they don't work

    routes.rb file

    DemoApp::Application.routes.draw do
          get "static_pages/home"
    
          resources :tasks
    
          resources :microposts
    
          resources :users
    
          root :to => 'static_pages#home'
        end
    

    what I want to do is link to the index page of each of these like the one here

    <h1>Listing tasks</h1>
    
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Status</th>
          <th></th>
          <th></th>
          <th></th>
        </tr>
      </thead>
    
      <tbody>
        <% @tasks.each do |task| %>
          <tr>
            <td><%= task.name %></td>
            <td><%= task.status %></td>
            <td><%= link_to 'Show', task %></td>
            <td><%= link_to 'Edit', edit_task_path(task) %></td>
            <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
    
    <br>
    
    <%= link_to 'New Task', new_task_path %>
    

    and also have a link back to the home page. sorry is this a funky question?