[英]Wrong url in the link_to helper
When I use link_to in rails 当我在rails中使用link_to
I know that the form is 我知道表格是
link_to "Profile", profile_path(@profile)
# => <a href="/profiles/1">Profile</a>
but in my code 但是在我的代码中
<% @posts.each do |post|%>
<h2><%=post.title %></h2>
<p><%= post.content%></p>
<%=link_to "show", posts_path(post.id) %>
<%end%>
I expect my url looks like posts/1
but it was posts.1
我希望我的网址看起来像
posts/1
但它是posts.1
Try <%=link_to "show", post_path(post) %>
尝试
<%=link_to "show", post_path(post) %>
post
not posts
. post
不是posts
。 See here http://guides.rubyonrails.org/routing.html 看到这里http://guides.rubyonrails.org/routing.html
link_to comes with syntax/signature link_to(name = nil, options = nil, html_options = nil, &block)
Which creates a link tag of the given name using a URL created by the set of options. link_to带有语法/签名
link_to(name = nil, options = nil, html_options = nil, &block)
使用由选项集创建的URL创建给定名称的链接标记。
Signatures; 签名;
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
I will take the same example from your question, 我将从你的问题中拿出同样的例子,
This link_to "Profile", profile_path(@profile)
creates path; 此
link_to "Profile", profile_path(@profile)
创建路径;
# => <a href="/profiles/1">Profile</a>
whereas, <%=link_to "show", posts_path(post.id) %>
creates 而
<%=link_to "show", posts_path(post.id) %>
创建
# => <a href="/profiles.1">show</a>
Other options to create appropriate routes are as follows; 创建适当路线的其他选项如下:
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>
Hope this could help you otherwise see this link_to apidoc 希望这可以帮助您另外看到此link_to apidoc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.