0

(几天来我很难从数据库中选择一个随机项目并能够进一步传递它。)

这是基本操作,我尝试了几种方法,给我带来了同样的问题,params {"id" = "random"} <- 控制器方法的名称

控制器:

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让我们说作为一个例子它工作它向我显示对象的内存#location。

错误日志:

 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 做

根.rb:

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

1 回答 1

0

如果您的路线很安静并且您对资源有 GET,则它需要一个 ID。如果您对集合执行 GET 操作,则不需要。我会确保随机看起来像

resources :tasks do
  get 'random', :on => :collection
end
于 2012-09-17T17:00:34.440 回答