Tadashi Mori
Tadashi Mori

Reputation: 118

Creating Links on Rails

I've just started using rails yesterday, so this is a kinda noob question

for example, a user is at www.example.com/name and I want to make several links to www.example.com/name/:id

So I tried something like this:

<% @items.each do |item| %>
<%= link_to item.name, '/name' :id %>
<% end %>

I know, it was a complete guess on how I should write the code, but the restful code sends to a completely wrong link. How should I write this three lines?

Upvotes: 1

Views: 108

Answers (2)

Robin
Robin

Reputation: 21884

Use the route helper:

<% @items.each do |item| %>
    <%= link_to item.name, item_path(item) %>
<% end %>

ps: when you have a simple question like this one, take a look at this guide, you'll often find the answer.

Upvotes: 2

ronalchn
ronalchn

Reputation: 12335

Try

<%= link_to item.name, item_path(item) %>

item_path is a URL helper method which spits out the link to show a name.

URL helpers have the general form:

{action}_{class}_path({object or object_id})

If {action}_ is omitted, then the default action is assumed (normally show).

Upvotes: 1

Related Questions