2010-05-13 59 views
1

所以我有一個用戶控制器,它有(其中包括)一個稱爲細節的功能。控制器功能查詢的軌道路由

想法是,用戶可以去 localhost:3000/user /:user_id/details 並能夠查看user_id的詳細信息。

例如,我有一個名爲「tester」的用戶。 當我去uri:http://localhost:3000/users/tester/details 我想要調用細節功能,呈現細節視圖,並顯示用戶測試儀的信息。

而是我得到一個錯誤說

No action responded to tester. Actions: change_password, create, current_user, details, forgot_password, index, login_required, new, redirect_to_stored, show, and update_attributes 

而且據我所知,基本上意味着,如果我想訪問的細節,我真的應該使用

http://localhost:3000/users/details 

除本ISN真的沒有用...>。 < 即不是把我送到http://localhost:3000/users/details/registries (這是我已經規定了任何人試圖查看users/:user_id,如此反覆的默認路徑,這是工作,我想要的方式它)

的一點是:任何人可以幫助並告訴我如何能讓 users/:user_id/details以我想要的方式工作並顯示以下詳細信息:user_id?

謝謝!

回答

2

您是否在使用資源?如果您的路線是這樣的:

map.resources :users 

你可以把它:

map.resources :users, :member => { :details => :get } 

這將允許URL /用戶/ GET請求:ID /細節

此處瞭解詳情:http://guides.rubyonrails.com/routing.html#customizing-resources

+0

@ Jty.tan:這個答案是更「常規」的做法,我認爲它應該是被接受的答案。 – 2010-05-13 09:43:40

+1

好的。 對不起,我真的很難找出一些這些東西。我仍然試圖讓我的腦袋圍繞着路線的想法和不同要求的目的。 現在關於上述情況,我是否可以假設它意味着細節函數應該在通過get請求時作爲用戶的成員來對待? 什麼是:收集呢?我也不明白這個指南。 – 2010-05-13 14:18:50

+0

並感謝迄今爲止的答覆,順便說一句。 :D – 2010-05-13 14:20:45

-1

爲了使路由如「users /:user_id/details」在routes.rb中更改以下內容

map.users 'users/:user_id/details', :controller => 'users', :action=>'details' 
+0

謝謝!我仍然在學習這些東西,所以感謝耐心! :D並感謝dylanfm! – 2010-05-13 08:29:59

0

我認爲你的問題是以這種方式設置路線,而不是:user_id你有:login(或其他)在url /users/tester而不是/users/34。可能你應該看看to_param1st example,2nd example,3rd example)。

如果你想在航線另一種選擇(除了默認REST路由),你如果你正在使用map.resources(@dylanfm答案)可以添加:member => {:details => :get}或只是映射它像@Salil答案。

+0

哦,我已經設置了'to_param',以便它給我:login而不是:user_id。 :) – 2010-05-13 14:19:50