Rails的路由:错了路
问题描述:
我是新来的Rails,现在学习路由,这是我在我的routes.rb
:Rails的路由:错了路
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'。任何人都可以弄清楚为什么?
谢谢。
答
听起来像文本是你的应用程序中的资源 - 你应该使用resource routing。
对于这种确切的情况,如果由于某种原因您不想使用资源路由,您应该查看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 %>