0

Working on a Ruby on Rails project using ERB. I am trying to link a user to their profile page. For example, clicking on the dropdown link and then profile should bring you to site.com/users/1 if you are logged in as User 1.

<a class="dropdown-item" href="users/<%= current_user.id %>">Profile</a

This works on all the pages, however when I move to the profile page, it for some reason changes to site.com/users/users/1 if I click on the Profile link while on the profile thus giving me an error. Any tips?

SJK
  • 135
  • 1
  • 10

2 Answers2

0

The rails way is to use link_to. It will automatically update the url based on your host.

So something like this must do the job:

<%= link_to "Profile", user_path(current_user) %>

<%= link_to current_user.email, user_path(current_user) %>
Yshmarov
  • 3,450
  • 1
  • 22
  • 41
0

It's better to follow the convention:

<%= link_to "Profile", user_path(current_user), class: "dropdown-item" %>

Another option is:

<a class="dropdown-item" href="<%= user_path(current_user) %>">Profile</a>

But if you insist on your way, try adding a slash (/) before "users/<%= current_user.id %>" as follows:

<a class="dropdown-item" href="/users/<%= current_user.id %>">Profile</a>
TamerB
  • 1,401
  • 1
  • 12
  • 27