简体   繁体   中英

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 %>

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM