2012-09-17 13 views
0

(我有一些艱難的時間,幾天來管理從德數據庫中選擇一個隨機項 ,並能夠進一步通過。)無法重定向到零! - 基本MVC操作

這是基本的操作和我嘗試幾種方法所有,把我帶到了 同樣的問題,則params { 「ID」= 「隨機」} < - 控制器的名稱 方法

控制器:

def random  # method works checked it in the console. Returns a valid obj. 
    @task = Task.find(rand(Task.first.id..Task.last.id)) 
    render "random" 
end 

random.html.erb <%= @task %>#我想讓它工作,如果我手動通過 讓#say ../random/1可以說作爲一個例子它的作品它顯示了我的 對象的內存#位置。

錯誤日誌:

ActionController::ActionControllerError in TasksController#show 
Cannot redirect to nil! 

Rails.root: /home/bogdan/ex/bored 
Application Trace | Framework Trace | Full Trace 

actionpack (3.2.3) lib/action_controller/metal/redirecting.rb:60:in 
`redirect_to' 
actionpack (3.2.3) lib/action_controller/metal/flash.rb:25:in 
`redirect_to' 
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:60:in 
`block in redirect_to' 
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `block 
in instrument' 
activesupport (3.2.3) 
lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (3.2.3) lib/active_support/notifications.rb:123:in 
`instrument' 
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:59:in 
`redirect_to' 
actionpack (3.2.3) lib/action_controller/metal/implicit_render.rb:4:in 
`send_action' 
actionpack (3.2.3) lib/abstract_controller/base.rb:167:in 
`process_action' 
. 
. 
. 
(only the top of the stack) 

Request 

Parameters: 

{"id"=>"random"} 

編輯: 無聊:: Application.routes.draw做

roots.rb:

to => 'tasks#main' 
    resources :tasks 
    match ':controller(/:action(/:id))(.:format)' 
+0

你的路線將有所幫助。這可能是問題的根源。 –

+0

你可以通過@test = Task.first來快速測試,從圖片中獲得更多的複雜性。如果這仍然是壞的,因爲它可能是,我敢打賭你的路線是RESTful而不是匹配 –

+0

添加routes.rb –

回答

0

如果你的路線寧靜,你有一個資源上的GET,它期望一個ID。如果您對該集合進行GET操作,則不需要。我會確保隨機看起來像

resources :tasks do 
    get 'random', :on => :collection 
end 
+0

這是假設'任務'是你的資源,當然:) –

+0

是的任務它是:P,但索引時我沒有:collection方法...我有@tasks = Task.find(:all)...我應該這樣做 –

相關問題