论坛首页 编程语言技术论坛

Rails routes mapping的一个奇怪的问题。顺便讨论一下如何做RESTful的paginate。

浏览 3863 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-18  
大家可以试验一下,在我的开发环境中会出现这个问题,不知道是不是只有我有问题。做法如下:
[list]
  • 随便建立一个Rails工程;
  • 随便生成一个scaffold_resource,比如map.resources :articles;
  • 在routes.rb中加入如下代码,添加自己的routes mapping:
  • map.articles_paginate "articles;paginate", :controller => 'articles', :action => 'paginate', :conditions => { :method => :get }
    
  • 在ArticlesController中声明方法paginate,然后随便以任何方式从view中调用这个url。
  • [/list]
    我这里产生的结果是unknow action。这说明routes mapping已经起作用了,但是找不到paginate方法。

    我知道我想要的mapping可以通过rails自带的方式实现,不需要自己写mapping。但是由于某些原因,我无法使用自带的实现方式。而且按道理我自己mapping也不应该有问题。请各位看看是怎么回事儿。

    另外,顺便讨论一下RESTful的paginate应该如何实现。OK,我知道有人会说不要为了REST而REST,要用在合适的地方。这我同意,就是想讨论一下。如果结论就是REST不适合用在paginate上,那就不用呗。
       发表时间:2007-07-18  
    该怎样实现还怎样实现
    比如TopicsController中:
    @post_pages, @posts = paginate(:posts, :per_page => 5, : order => 'posts.created_at', :conditions => ['posts.topic_id = ?', params[:id]])
    

    show.rhtml中:
    <% if @post_pages and @post_pages.current.next %>
      <%= link_to "Next page", { :page => @post_pages.current.next }  %>
    <% end %>
    
    <% if @post_pages and @post_pages.current.previous %>
      <%= link_to "Previous page", { :page => @post_pages.current.previous }  %>
    <% end %>
    
    <% if @post_pages.page_count > 1 %>
      <%= _('Pages:') %>
        <%= pagination_links @post_pages, :window_size => 10 %><% end %>
    

    参考Beast的源码即可
    0 请登录后投票
       发表时间:2007-07-18  
    hideto 写道
    该怎样实现还怎样实现,参考Beast的源码即可。

    那你能解释一下为什么我自己加的mapping会找不到action吗?谢谢。
    0 请登录后投票
       发表时间:2007-07-18  
    hideto 写道
    该怎样实现还怎样实现
    比如TopicsController中:
    @post_pages, @posts = paginate(:posts, :per_page => 5, : order => 'posts.created_at', :conditions => ['posts.topic_id = ?', params[:id]])
    

    参考Beast的源码即可


    我在自己的代码中使用paginate,
      def index
        # @articles = Article.find(:all)
        @article_pages, @articles = paginate :articles, :per_page => 1
        respond_to do |format|
          format.html # index.rhtml
          format.xml  { render :xml => @articles.to_xml }
        end
      end
    

    但是出现了如下问题:
    ArgumentError in ArticlesController#index

    wrong number of arguments (2 for 0)

    RAILS_ROOT: /Users/benben/Documents/workspace/playground/config/..
    Application Trace | Framework Trace | Full Trace

    #{RAILS_ROOT}/app/controllers/articles_controller.rb:6:in `paginate'
    #{RAILS_ROOT}/app/controllers/articles_controller.rb:6:in `index'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/bin/mongrel_rails:16:in `load'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/bin/mongrel_rails:16

    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `send'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `synchronize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:in `process_client'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `each'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `process_client'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `initialize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `new'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `initialize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `new'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:271:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `each'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243

    #{RAILS_ROOT}/app/controllers/articles_controller.rb:6:in `paginate'
    #{RAILS_ROOT}/app/controllers/articles_controller.rb:6:in `index'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `send'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `synchronize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `process'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:in `process_client'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `each'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `process_client'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `initialize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `new'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `initialize'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `new'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:271:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `each'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in `run'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/bin/mongrel_rails:16:in `load'
    /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/powerpc/bin/mongrel_rails:16

    Request

    Parameters: None

    Show session dump

    ---
    flash: !map:ActionController::Flash::FlashHash {}


    Response
    Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
    0 请登录后投票
       发表时间:2007-07-18  
    现在更奇怪,连这样的标准写法都不行了...
    map.resources :articles, :collection => { :paginate => :get }
    
    0 请登录后投票
       发表时间:2007-07-18  
    找到原因了,paginate是ActionController::Pagination中的一个方法的名字,在ActionController中是hidden action。换个名字就好了,哎。
    0 请登录后投票
    论坛首页 编程语言技术版

    跳转论坛:
    Global site tag (gtag.js) - Google Analytics