`

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

阅读更多
大家可以试验一下,在我的开发环境中会出现这个问题,不知道是不是只有我有问题。做法如下:
[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上,那就不用呗。
    分享到:
    评论
    5 楼 AllenYoung 2007-07-18  
    找到原因了,paginate是ActionController::Pagination中的一个方法的名字,在ActionController中是hidden action。换个名字就好了,哎。
    4 楼 AllenYoung 2007-07-18  
    现在更奇怪,连这样的标准写法都不行了...
    map.resources :articles, :collection => { :paginate => :get }
    
    3 楼 AllenYoung 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"}
    2 楼 AllenYoung 2007-07-18  
    hideto 写道
    该怎样实现还怎样实现,参考Beast的源码即可。

    那你能解释一下为什么我自己加的mapping会找不到action吗?谢谢。
    1 楼 hideto 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的源码即可

    相关推荐

      RESTful Rails Development

      ActiveResource 是 Rails 提供的一个库,用于实现 RESTful 客户端。它可以方便地与 RESTful 服务器交互。 #### 1.17 最终总结 通过上述讨论,可以看出 RESTful 开发不仅是一种技术实践,也是一种设计理念。通过...

      RestFul_Rails_Dev_pdf_v_0.1.zip

      RESTful设计的一个关键特性是使用动词和名词来构建URL。例如,`/users`表示用户资源,而`/users/1`表示ID为1的具体用户。对于CRUD(Create, Read, Update, Delete)操作,Rails提供了如下的默认映射: - GET /users...

      How to RESTful rails

      Rails的RESTful控制器通常遵循这些约定,例如,对于一个用户资源,其URL可能为`/users/1`,通过GET请求可以获取该用户的详情,而PUT或PATCH请求则可以更新用户信息。 ### RESTful路由与视图 在Rails中,RESTful...

      rails-routes:在Rails应用程序上启用configroutes * .rb

      Rails ::路线 您的Rails应用程序上的路线太多?...将这个gem添加到您的项目后,您可以在config/routes创建多个路由文件。 只要确保您用 # config/routes/v1.rb Rails . application . routes . draw do

      RestFul_Rails_Dev_v_0.1

      ActiveResource 是 Rails 提供的一个库,用于构建 RESTful 客户端,使得与远程服务的交互变得更加简单。 **ActiveResource 示例**: ```ruby class Task self.site = "http://example.com" end ``` 这段代码...

      Rails 101 入门电子书

      - 测试安装: 创建一个简单的Rails应用来验证是否成功安装。 #### 五、练习作业0-Hello World - **目标**: - 学习如何创建第一个Rails应用程序。 - **过程**: - 创建新项目。 - 设置数据库配置。 - 创建控制器...

      关于Rails登录和验证插件http_authentication restful-authentication

      Rails是一个流行的开源Web应用程序框架,基于Ruby编程语言。在Rails应用中实现用户登录和验证是构建任何Web服务的基础。本文将深入探讨Rails中的http_authentication和restful-authentication插件,这两种方法都常...

      RESTful.Rails.Development.2015.10.pdf

      根据提供的文件信息,我们可以推断出这是一本关于如何使用Ruby on Rails框架来构建RESTful应用程序和服务的专业书籍。书名为《RESTful Rails Development》,作者为Silvia Puglisi,出版时间为2015年10月。接下来,...

      will_paginate 2.2.2

      `will_paginate`是Rails框架中非常流行的一个分页插件,版本2.2.2是它的一个历史版本。在本文中,我们将深入探讨`will_paginate`的使用方法、功能特性以及它如何与Rails集成,帮助提升Web应用的性能和用户体验。 一...

      Rails项目源代码

      这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何利用Rails的强大功能来创建一个允许用户上传、分享和浏览图片的应用。 1. **Rails框架基础**: Rails的核心理念是DRY(Don't...

      Rails101_by_rails4.0

      Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它遵循模型-视图-控制器(MVC)的架构模式,设计用来快速开发数据库驱动的动态网页。随着Rails版本的更新迭代,此书聚焦于一个特定的版本,帮助...

      routes源代码

      Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that ...

      Rails上的API:使用Rails构建REST APIAPIs on Rails: Building REST APIs with Rails

      在本篇内容中,我们将深入探讨如何利用Ruby on Rails(简称Rails)这一强大的Web应用程序框架来构建可伸缩且易于维护的RESTful API。Rails以其简洁优雅的语法、高效的开发速度以及良好的社区支持而闻名,这使得它...

      rails指南 中文版

      Rails指南中文版是针对Ruby on Rails框架的一份详尽教程,旨在帮助开发者深入理解并熟练掌握这个强大的Web应用开发工具。Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-...

      RESTful-Rails-Development:RESTful Rails 开发库

      RESTful-Rails-开发 RESTful Rails 开发库。 你好 Rails 类别图 API - WIKICAT 本地图片 带有地理标记的维基百科文章 - WIKIPINS 城市步道API 余烬漫步

      Rails相关电子书汇总

      Ruby on Rails,通常简称为Rails,是一个基于Ruby语言的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,以简洁、高效的代码和“约定优于配置”的理念著称。此压缩包中的"rubyonrails21-cn.pdf"可能是...

    Global site tag (gtag.js) - Google Analytics