繁体   English   中英

link_to帮助程序中的URL错误

[英]Wrong url in the link_to helper

当我在rails中使用link_to

我知道表格是

link_to "Profile", profile_path(@profile)
# => <a href="/profiles/1">Profile</a>

但是在我的代码中

<% @posts.each do |post|%>
  <h2><%=post.title %></h2>
  <p><%= post.content%></p>
  <%=link_to "show", posts_path(post.id) %>
<%end%>

我希望我的网址看起来像posts/1但它是posts.1

尝试<%=link_to "show", post_path(post) %>

post不是posts 看到这里http://guides.rubyonrails.org/routing.html

link_to带有语法/签名link_to(name = nil, options = nil, html_options = nil, &block)使用由选项集创建的URL创建给定名称的链接标记。

签名;

link_to(body, url, html_options = {})
  # url is a String; you can use URL helpers like
  # posts_path

link_to(body, url_options = {}, html_options = {})
  # url_options, except :method, is passed to url_for

link_to(options = {}, html_options = {}) do
  # name
end

link_to(url, html_options = {}) do
  # name
end

我将从你的问题中拿出同样的例子,

link_to "Profile", profile_path(@profile)创建路径;

# => <a href="/profiles/1">Profile</a>

<%=link_to "show", posts_path(post.id) %>创建

# => <a href="/profiles.1">show</a>

创建适当路线的其他选项如下:

link_to "Profile", @profile
# => <a href="/profiles/1">Profile</a>

link_to "Profile", controller: "profiles", action: "show", id: @profile
# => <a href="/profiles/show/1">Profile</a>

希望这可以帮助您另外看到此link_to apidoc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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