TypeError:无法将Post转换为Array

时间:2010-09-03 01:08:07

标签: ruby-on-rails model controller ruby-on-rails-3 will-paginate

我正在使用Rails 3和will_paginate。分页在我的帖子控制器中运行正常但在我的标签控制器中我在第二个标签ID上出错。这是错误:

TypeError in TagsController#show

can't convert Post into Array

...

app/controllers/tags_controller.rb:8:in `show'

tags_controller.rb#显示:

def show
  @tag = ActsAsTaggableOn::Tag.find(params[:id])
  @posts = Post.paginate(Post.tagged_with(@tag), :page => params[:page], :order => "updated_at DESC") # line 8
  respond_with(@posts)
end

令我感到困惑的是,此代码在posts控制器中起作用:

def index     @posts = Post.paginate:page => params [:page],:order => “updated_at DESC”   端

我的帖子和标签模型包括:

cattr_reader :per_page
@@per_page = 10

有谁能解释我在这里做错了什么?

编辑:(完整堆栈跟踪)

will_paginate(3.0.pre2)lib / will_paginate / collection.rb:134:in replace' will_paginate (3.0.pre2) lib/will_paginate/collection.rb:134:in replace'     will_paginate(3.0.pre2)lib / will_paginate / finders / active_record.rb:91:in wp_query' will_paginate (3.0.pre2) lib/will_paginate/finders/base.rb:64:in block in paginate'     will_paginate(3.0.pre2)lib / will_paginate / collection.rb:87:in create' will_paginate (3.0.pre2) lib/will_paginate/finders/base.rb:62:in paginate'     app / controllers / tags_controller.rb:8:show' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in send_action'     actionpack(3.0.0)lib / abstract_controller / base.rb:150:in process_action' actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:in process_action'     actionpack(3.0.0)lib / abstract_controller / callbacks.rb:18:in block in process_action' activesupport (3.0.0) lib/active_support/callbacks.rb:435:in _ run__833165888__process_action__116844887__callbacks'     activesupport(3.0.0)lib / active_support / callbacks.rb:409:in _run_process_action_callbacks' activesupport (3.0.0) lib/active_support/callbacks.rb:93:in run_callbacks'     actionpack(3.0.0)lib / abstract_controller / callbacks.rb:17:在process_action中的process_action' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:30:in块中     activesupport(3.0.0)lib / active_support / notifications.rb:52:in block in instrument' activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in instrument'     activesupport(3.0.0)lib / active_support / notifications.rb:52:in instrument' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:29:in process_action'     actionpack(3.0.0)lib / action_controller / metal / rescue.rb:17:in process_action' actionpack (3.0.0) lib/abstract_controller/base.rb:119:in process'     actionpack(3.0.0)lib / abstract_controller / rendering.rb:40:in process' actionpack (3.0.0) lib/action_controller/metal.rb:133:in dispatch'     actionpack(3.0.0)lib / action_controller / metal / rack_delegation.rb:14:in dispatch' actionpack (3.0.0) lib/action_controller/metal.rb:173:in block in action'     actionpack(3.0.0)lib / action_dispatch / routing / route_set.rb:62:in call' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in dispatch'     actionpack(3.0.0)lib / action_dispatch / routing / route_set.rb:27:in call' rack-mount (0.6.12) lib/rack/mount/route_set.rb:148:in block in call'     rack-mount(0.6.12)lib / rack / mount / code_generation.rb:93:in block in recognize' rack-mount (0.6.12) lib/rack/mount/code_generation.rb:89:in optimized_each'     rack-mount(0.6.12)lib / rack / mount / code_generation.rb:92:in recognize' rack-mount (0.6.12) lib/rack/mount/route_set.rb:139:in call'     actionpack(3.0.0)lib / action_dispatch / routing / route_set.rb:492:in call' actionpack (3.0.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in call'     actionpack(3.0.0)lib / action_dispatch / middleware / head.rb:14:in call' rack (1.2.1) lib/rack/methodoverride.rb:24:in call'     actionpack(3.0.0)lib / action_dispatch / middleware / params_parser.rb:21:in call' actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:in call'     actionpack(3.0.0)lib / action_dispatch / middleware / session / abstract_store.rb:149:in call' actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:in call'     activerecord(3.0.0)lib / active_record / query_cache.rb:32:in block in call' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in cache'     activerecord(3.0.0)lib / active_record / query_cache.rb:12:in cache' activerecord (3.0.0) lib/active_record/query_cache.rb:31:in call'     activerecord(3.0.0)lib / active_record / connection_adapters / abstract / connection_pool.rb:355:in call' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:in阻止调用'     activesupport(3.0.0)lib / active_support / callbacks.rb:415:_run_call_callbacks' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:in调用'     rack(1.2.1)lib / rack / sendfile.rb:107:in call' actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:in call'     actionpack(3.0.0)lib / action_dispatch / middleware / show_exceptions.rb:46:in call' railties (3.0.0) lib/rails/rack/logger.rb:13:in call'     rack(1.2.1)lib / rack / runtime.rb:17:in call' activesupport (3.0.0) lib/active_support/cache/strategy/local_cache.rb:72:in call'     rack(1.2.1)lib / rack / lock.rb:11:in block in call' <internal:prelude>:10:in synchronize'     rack(1.2.1)lib / rack / lock.rb:11:in call' actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:in call'     railties(3.0.0)lib / rails / application.rb:168:in call' railties (3.0.0) lib/rails/application.rb:77:in method_missing'     railties(3.0.0)lib / rails / rack / log_tailer.rb:14:in call' rack (1.2.1) lib/rack/content_length.rb:13:in call'     rack(1.2.1)lib / rack / handler / webrick.rb:52:in service' /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in service'     /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in阻止在start_thread'

1 个答案:

答案 0 :(得分:1)

你试过了吗?

@posts = Post.tagged_with(@tag).paginate :page => params[:page], :order => "updated_at DESC"