2012-09-29 128 views
1

我是新來的Rails,現在學習路由,這是我在我的routes.rbRails的路由:錯了路

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'。任何人都可以弄清楚爲什麼?

謝謝。

回答

1

聽起來像文本是你的應用程序中的資源 - 你應該使用resource routing

對於這種確切的情況,如果由於某種原因您不想使用資源路由,您應該查看rake routes的輸出並查看分配給您的text#show路由的名稱並使用它。

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