0

I have an anchor tag in my rails app..

<a href='app/users/<%= current_user.id %>/personal_settings'> <i class='icon-gear'></i> Personal Settings </a>

thing is this gets rendered in many of my html pages. So whenever I click this <a> tag the url that goes is like parent_url + app/users/:id/personal_settings Is there any way I can prevent this. Whenever I click this a tag the url should be just app/users/:id/personal_settings. no parent tags should come.

richsinn
  • 1,341
  • 1
  • 12
  • 27
Nidhin S G
  • 1,685
  • 2
  • 15
  • 45
  • 1
    Can you run `rake routes` and show us the output for this particular route? – richsinn Sep 17 '14 at 08:28
  • Never use hardcoded url. Always use routing helper. the answer mentioned by Mohamed Yakout is correct. – jbmyid Sep 17 '14 at 09:11
  • @jbmyid For the record, I'm not using hardcoded URLs. My solution is also a very viable solution if you don't want to use the `link_to` helper method. – richsinn Sep 18 '14 at 01:16
  • @richsinn so even if you dont want to use link_to method then also you can use routing helper eg. ` link` – jbmyid Sep 18 '14 at 05:46
  • @jbmyid you obviously did not read my answer. I did use the routing helper (also called a named route). In fact Mohamed Yakout copied my answer after I first posted the usage of the named route. Mohamed's original answer used a hardcoded URL, and I recommended using a named route (you can still see his original answer using the hardcoded URL). Even the example named route `users_personal_settings_path` is my own example I made up that Mohamed copied in his answer! The only extra thing that Mohamed added was the usage of the `link_to` helper. – richsinn Sep 18 '14 at 06:23
  • @jbmyid and even your own example in your comment is a copy from my answer! – richsinn Sep 18 '14 at 06:30
  • @richsinn Yes off-course i used your route helper to give an example. And sorry I dint wanted to direct you but Nidhin. I misunderstood who asked question. And my first comment was not for you but for Nidhin. And i suggested answer of mohamed bcs of `link_to` and `block` usage. – jbmyid Sep 18 '14 at 06:53
  • @richsinn And one more thing no body copied your answer. is just helper name and not the code. – jbmyid Sep 18 '14 at 06:55

2 Answers2

2

In your anchor tag, I'd recommend using the named route for the url in your anchor tag's href (hint: run rake routes to find what your named route is, or you can set your own named route in your routes.rb file) and pass in the current_user.id as the parameter to the named route.

For example, if your named route is users_personal_settings, then here's how I would set up your <a> tag (note the _path extension to the named route) :

<a href='<%= users_personal_settings_path(current_user.id) %>'> 
  <i class='icon-gear'></i> Personal Settings 
</a>
Community
  • 1
  • 1
richsinn
  • 1,341
  • 1
  • 12
  • 27
1

You need to put / before your url for relative url to root url as the following code:

<a href='/app/users/<%= current_user.id %>/personal_settings'> <i class='icon-gear'></i> Personal Settings </a>

Or you can use absolute url as the following:

<a href='https://www.localhost:3000/app/users/<%= current_user.id %>/personal_settings'> <i class='icon-gear'></i> Personal Settings </a>

Read about href for absolute, and relative url.


You can use helper method link_to as the following:

<%= link_to(users_personal_settings(current_user)) do %>
  <i class='icon-gear'></i> "Personal Settings"
<% end %>
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45