1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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).

1 Comment

Ok, but now the links are like www.example.com/name.id, cant it be like www.example.com/name/id?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.