无法重定向到零! - 基本的MVC操作

时间:2012-09-17 16:47:24

标签: ruby-on-rails-3 model-view-controller

(我有几天难以设法选择一个随机项目 来自数据库,并能够进一步传递。)

这是基本操作,我尝试了几种方式,把我带到了 同样的问题,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做

roots .rb:

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

1 个答案:

答案 0 :(得分:0)

如果您的路由是restful并且您对资源进行了GET,那么它需要一个ID。如果您对集合进行GET,则不需要。我确保随机看起来像

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