0

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

mabarroso
  • 659
  • 2
  • 11
  • 23
rails007
  • 45
  • 1
  • 9

3 Answers3

2

show_user_path would work, it's just more verbose.

Welcome to the magic of Ruby on Rails.

Macdiesel
  • 935
  • 1
  • 9
  • 19
0

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.

a.s.t.r.o
  • 3,261
  • 5
  • 34
  • 41
0

It is a convention in Ruby on Rails, you can use show_user_path(user), too.

code4j
  • 4,208
  • 5
  • 34
  • 51