2011-11-26 55 views
0

我創建了一個新的控制器,並添加了一個新的動作(my_new_action)。對routes.rb我添加了此操作的路由,但是當我設置爲URL瀏覽器地址localhost:3000/users/my_new_action時,我將得到主題中的錯誤。Rails 3 - 找不到<controller>沒有ID

class UsersController < ApplicationController 
    # .... 

    def my_new_action 
    end 
end 

請問誰能幫助我,請問如何解決?

感謝

編輯

resources :users do 
    collection do 
     get 'my_new_action' 
    end 
    end 

EDIT2 這是從終端的精確輸出:

Started GET "https://stackoverflow.com/users/my_new_action" for 127.0.0.1 at Sat Nov 26 13:55:50 +0100 2011 
    Processing by UsersController#my_new_action as HTML 
filter_access_to tried to find User from params[:id] (nil), because attribute_check is enabled and @user isn't set, but failed: ActiveRecord::RecordNotFound: Couldn't find User without an ID 
Completed 404 Not Found in 1ms 

ActiveRecord::RecordNotFound (Couldn't find User without an ID): 


Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms) 
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms) 
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.0ms) 
+0

顯示'routes.rb' – Hauleth

+0

我更新了我的問題。 – user984621

+1

我懷疑這是你得到的確切的錯誤信息 - 你能發佈整個異常嗎? – deviousdodo

回答

2

標題是誤導性的,你已經把<controller>因爲你大概看到User,但它很可能是指您正在嘗試構建或使用的模型。因爲你沒有粘貼更多的代碼,我不能確切地說出問題的具體位置,而是尋找你在代碼中使用的id的引用 - 這很可能是空的。

+0

好吧,我只有empy行動在我的控制器中,只有行動的定義,我對這一行動的看法也是空洞的。我期望這足以創建一個新的動作...我不想使用ID的引用...只是創建一個新的「頁面」(+我更新了我的問題) – user984621

+0

而在更新中,您有錯誤拼寫爲你:'filter_access_to試圖從params [:id](nil)找到用戶,因爲attribute_check已啓用,@user未設置,但失敗:ActiveRecord :: RecordNotFound:無法找到沒有ID的用戶' – deviousdodo

+0

但如何可能,如果我設置URL **用戶/新**,一切正常? – user984621

0

由於路由無法正常工作,代碼顯示URL/users/my_new_action嘗試爲默認'show'找到'/ users/my_new_action'的用戶標識'行動。

我會代替你喜歡的東西(請沒有這不是測試代碼)提供的路線代碼...

resources :users do 
    match '/my_new_action', :controller => 'users', :action => 'my_new_action', :as => :mynewaction 
end