2011-11-28 186 views
1

在我的項目中,故事屬於用戶,通常所有故事的訪問都是通過嵌套資源訪問,除了顯示全局列表所有的故事。在我的routes.rb文件我有:Rails 3功能測試 - 'get()'無法訪問控制器方法

resources :users do 
    resources :stories 
end 
match "/all_stories" => "stories#all_stories" 

match語句生成以下路線:

all_stories /all_stories(.:format) {:controller=>"stories", :action=>"all_stories"} 

在我stories_controller.rb文件我有一個名爲動作:all_stories

def all_stories 
    @stories = Story.all 
    end 

從我的觀點,我以這種方式調用link_to

<%= link_to "All Stories", all_stories_path %> 

造成此錯誤:

No route matches {:action=>"show", :controller=>"stories", :user_id=>nil, 
:id=>#<Story id: 1, title: "test story 01", desc: "Edited desc for test story 01", 
activity: 20, created_at: "2011-11-28 01:07:08", updated_at: "2011-11-28 01:26:31", 
user_id: 5>} 

在我stories_controller_test.rb以下GET呼籲:all_stories生成上市錯誤:

get('all_stories') 
Undefined method `story_path' for #<#<Class:0x0000010430da18>:0x00000101789d10> 

get(:all_stories) 
undefined method `story_path' for #<#<Class:0x00000102a178b0>:0x00000100ee9cc8> 

get('/all_stories') 
No route matches {:controller=>"stories", :action=>"/all_stories"} 

get({:action=>"all_stories"}) 
No route matches {:controller=>"stories", :action=>"{:action=>\"all_stories\"}"} 

在前兩個,沒有'story_path「,因爲故事嵌套在用戶下。不知道接下來兩個會發生什麼。在我的控制器中,我可以使用什麼語法來getall_stories操作?

回答

1

嘗試改變路線

match "/all_stories" => "stories#all_stories", :as => :all_stories 

然後在你的代碼,你可以參考all_stories_pathall_stories_url

+0

所羅門,感謝您的建議,但它沒有奏效。我做了你的改變,並設置我的link_to/get方法引用:all_stories_path。我的link_to語句(在視圖中)出錯:沒有路由匹配{:action =>「show」,:controller =>「stories」,:user_id => nil,:id =>#}我的功能測試錯誤與:無路線匹配{:controller =>」stories「,:action =>」/ all_stories「} –

+1

嘗試將您的匹配所有故事線放在資源部分上方。如果這不起作用,你的link_to語句是什麼(也許有錯字)?最後,爲什麼你不添加你的控制器代碼,因爲也許裏面有東西...... – Solomon

+1

我在你的評論中再次看到錯誤,它似乎指向link_to中的問題。在all_stories_path中,您不應該將故事作爲:id參數傳遞。可能這不是all_stories路徑的問題,就像您爲每個單獨故事顯示的行中的問題一樣。儘量不要在頁面上顯示任何信息以檢查頁面是否正確路由。 – Solomon

0

我有這個問題,但我能夠使用@Controller 使用來解決它@控制