2013-04-30 42 views
0

美好的一天,這裏有很多關於這個問題的問題,但他們不幫助我。「顯示」動作的路由錯誤

對於signed_in用戶我有一個主頁bookmarksfolders,其中包含屬於此文件夾的書籤。

home方法是static_pages控制器,它看起來是這樣的:

def home 
    if signed_in? 
     @folder = current_user.folders.build 
     @folders = current_user.folders.all 
     @bookmark = current_user.bookmarks.build 
     @bookmarks = current_user.bookmarks.all 
    end 
    end 

這是home.html.erb

<div class="span3"> 
     <table class="table table-hover table-condensed"> 
      <%= render @bookmarks %> 
     </table> 
     </div> 
     <div class="span3"> 
     <table class="table table-hover table-condensed"> 
      <%= render @folders %> 
     </table> 
     </div> 

所以部分,我有2代表與所有bookmarks和所有folders,屬於current_user

這是_folder.html.erbfolders文件夾中views

<tr data-toggle="tooltip" data-placement="right" data-html="true"> 
    <td data-href="<%= folder %>" class="bookmark"><i class="icon-folder-close"></i> 
    <%= link_to folder.name, folder_path %> 
    </td> 
    <td> 
    <%= link_to '<i class="icon-edit"></i>'.html_safe, '#' %> 
    </td> 
    <td> 
    <%= link_to '<i class="icon-trash"></i>'.html_safe, folder_path, method: :delete, 
       data: {confirm: 'Are you sure?'}, 
       title: 'Delete folder ' + folder.name.to_s %> 
    </td> 
</tr> 

folders_controller.rb,與show方法:

def show 
    @folder = Folder.find(params[:id]) 
    @bookmarks = @folder.bookmarks.all 
    end 

resources :folders存在routes.rb

所有我需要的是去localhost:3000/folder/1,在主頁上folder_name點擊時。

這就是我得到:

Routing Error 
No route matches {:action=>"show", :controller=>"folders"} 

rake routes一切正常。

當在瀏覽器中放入localhost:3000/folder/1時,它工作正常。

謝謝!

回答

2

嘗試更換:

<%= link_to folder.name, folder_path %> 

<%= link_to folder.name, folder_path(folder) %> 

,因爲你需要提供ID的演出動作。

你可以得到更多的信息here

+0

我已經試過這一點,但它並不能幫助我。現在,當點擊'folder_name'時,我在地址欄中看到:'http:// localhost:3000 /#',但它仍然不訪問'folder/1'。 – 2013-05-01 05:18:57

+0

嘗試'folder_path(folder.id)'或'folder_path(:id => folder.id)' – Wasi 2013-05-01 13:31:48