ramijames ramijames - 1 year ago 39
Ruby Question

Rails 5 is adding a "." to a link_to path instead of a "/"

I'm trying out Rails 5 and have come across something weird. Everywhere that I've googled says that my code is right. Not sure what I'm doing wrong.

My view code:

<% provide(:title, "View all Users") %>

<% @users = User.all %>

<% @users.each do |user| %>
<% @user = user %>
<li><%= link_to user.name, users_path(@user) %><%= user.name %></li>
<% end %>

Spits out html:

<a href="/users.3">boop boop</a>

Notice that the path is using a dot instead of a forward slash.

Anyone have any idea why?

Answer Source

I believe the issue is the users_path(@user). You have an extra s there, try user_path(@user) or even better try @user

I would write that line as <li><%= link_to user.name, @user %><%= user.name %></li>

Rails is smart enough to understand that linking an object means you want to go to the show page for the object