2

I trying to achieve something like as below :-

<%= link_to "<%= user.email %>", users_path %>   

So that each user's email id is to be used as a hyper link to their user profile page.

How to achieve this ? I'm using rails 3.0.1.

Thanks in Advance

2 Answers 2

4

Don is close, but users_path is (i presume) the index action of a UsersController, so you don't need (and in fact shouldn't pass) user into the helper.

link_to is actually a helper method, which accepts a few arguments. The first argument is the text of the link itself (what goes in between <a> and </a>). You don't need to use the ERB syntax you are trying to use, just pass whatever text you want (user.email, or user.name, whatever).

<%= link_to user.email, user_path(user) %>
Sign up to request clarification or add additional context in comments.

3 Comments

@Saran: your question is for Rails 3, but if you ever use Rails 2, don't forget to html escape : use h(user.email) instead of user.email
By default, users_path goes to an #index action, so this answer actually will link everybody's email address to the (presumably) "all users" page. Furthermore, pace Don below, user_path(user) is a synonym for user in this example, so the whole thing can be simplified as <%= link_to user.email, user %>.
@Eric Hill Yes, you're right. I made the mistake of using users_path because he used it in his original example, even though he said in his question he wanted to link to an individual user page instead of the all users page. Updated to correct answer. Thanks!
3

Try the following, since you want a link to a user and not the index of all users

<%= link_to user.email, user_path(user) %>

Comments

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.