Search code examples
ruby-on-railsscaffolding

Getting Started with Ruby on Rails - Scaffold


I am new to rails.

When I generate scaffold for users , in products/index.html i have this code

<h1>Listing users</h1>

<table>
  <tr>
    <th>Name</th>
  </tr>

<% @users.each do |user| %>
  <tr>
    <td><%=h user.name %></td>
    <td><%= link_to 'Show', user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New user', new_user_path %>

-------------------------------------------------------------------------------------------------------------------------------

My doubt is in link_to tags,

<%= link_to 'Show', user %>
<%= link_to 'Edit', edit_user_path(user) %>
<%= link_to 'New user', new_user_path %>

Why not show_user_path(user) ? for first link 'Show' Any help would be apprecated


Solution

  • While generating a scaffold, you created the resource "user", that's why you can use it "as is" in link_to: rails knows that you want to see the resource user. show_user_path(user) links to the controller show action directly.