2013-07-12 66 views
0

我可以在應用程序中顯示上一個和下一個鏈接,但我試圖弄清楚如果我的鏈接有嵌套路由會發生什麼?Rails:處理嵌套路由時的上一個/下一個記錄

例如我的鏈接是

localhost:3000/users/1/photos/2 

我在我的模型有這些:

def previous_img 
    self.class.first(:conditions => ["created_at < ?", created_at], :order => "created_at desc") 
    end 

    def next_img 
    self.class.first(:conditions => ["created_at > ?", created_at], :order => "created_at asc") 
    end 

,並在我的意見我有這樣的:

<%= link_to "Previous", @photo.previous_img if @photo.previous_img %> 
    <%= link_to "Next", @photo.next_img if @photo.next_img %> 

這些鏈接將直接我到沒有users/1的鏈接,它只會給我

localhost:3000/photos/1 # for previous 
    localhost:3000/photos/3 # for next 

有沒有辦法,也許我可以連接users/1或實際的方法來獲得嵌套路線在這件事上?

感謝

+0

可能重複[鋼軌:「下一篇」在我型我秀觀「上一篇」鏈接,怎麼以?](http://stackoverflow.com/questions/1275963/rails-next-post-and-previous-post-links-in-my-show-view-how-to) –

回答

1

你應該能夠使用user_photo_path得到一個嵌套鏈接:

<%= link_to "Previous", user_photo_path(@photo.user, @photo.previous_img) if @photo.previous_img %> 
+0

正是我所需要的!謝謝。 – hellomello

+0

快速提問。我意識到,當用戶標識沒有記錄時,我收到一個錯誤。有沒有簡單的方法來找到用戶ID的記錄? – hellomello

+0

現在,當我嘗試檢查其他用戶的照片時,出現錯誤...我收到一個耙路由錯誤。即:'用戶/ 5 /照片/ 6',我檢查了這個鏈接確實工作,當我從視圖文件中刪除鏈接 – hellomello