[英]Rails routing: wrong path
我是Rails的新手,现在正在学习路由,这是我routes.rb
:
match '/text' => 'text#index'
match '/text/:id' => 'text#show'
这是我的app/views/text/index.html.erb
:
<h1>Texts</h1>
<% @texts.each do |t| %>
<div><%= link_to t.title, text_path(t) %></div>
<% end %>
问题是,当我单击链接时,它会将我重定向到“ /text.1”而不是“ / text / 1”。 谁能弄清楚为什么?
谢谢。
听起来文本是应用程序中的一种资源-您应该使用资源路由 。
对于这种确切的情况,如果由于某种原因您不想使用资源路由,则应查看rake routes
的输出,并查看为text#show
路由分配了什么名称并使用它。
try this
<h1>Texts</h1>
<% @texts.each do |t| %>
<div><%= link_to t.title, t %></div>
<% end %>
或这个
<h1>Texts</h1>
<% @texts.each do |t| %>
<div><%= link_to t.title, text_path(:id => t.id) %></div>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.