我正在阅读这两页

  • resources
  • Adding more RESTful actions

  • Rails指南页面显示
    map.resources :photos, :new => { :upload => :post }
    

    及其对应的URL
    /photos/upload
    

    这看起来很棒。

    我的routes.rb显示了这个
    map.resources :users, :new => { :signup => :get, :register => :post }
    

    当我这样做时:[~/my_app]$ rake routes
    我看到增加了两条新路线
      signup_new_user GET    /users/new/signup(.:format)
    register_new_user POST   /users/new/register(.:format)
    

    注意包含/new!我想要那个。我只想要/users/signup/users/register(如《 Rails路由指南》中所述)。

    有什么帮助吗?

    最佳答案

    将 Controller 公开为资源时,将自动添加以下操作:

    show
    index
    new
    create
    edit
    update
    destroy
    

    这些操作可以分为两类:
  • :member操作

  • 成员操作的URL具有目标资源的ID。例如:
    users/1/edit
    users/1
    

    您可以将:member操作视为类的实例方法。它始终适用于现有资源。

    默认成员操作:showeditupdatedestroy
  • :collection操作
  • :collection操作的URL不包含目标资源的ID。例如:
    users/login
    users/register
    

    您可以将:collection操作视为类的静态方法。

    默认收集 Action :indexnewcreate
    在您的情况下,您需要两项新的注册操作。这些 Action 属于:collection类型(因为提交这些 Action 时您没有用户的ID)。您的路线可以如下:
    map.resources :users, :collection => { :signup => :get, :register => :post }
    

    操作的URL如下:
    users/signup
    users/register
    

    如果要删除Rails生成的标准 Action ,请使用:except /:only选项:
    map.resources :foo, :only => :show
    
    map.resources :foo, :except => [:destroy, :show]
    

    编辑1

    我通常将confirmation Action 视为:member Action 。在这种情况下,params[id]将包含确认码。

    路由配置:
    map.resources :users, :member => { :confirm => :get}
    

    URL
    /users/xab3454a/confirm
    
    confirm_user_path(:id => @user.confirmation_code) # returns the URL above
    

    Controller
    class UsersController < ApplicationController
      def confirm
        # assuming you have an attribute called `confirmation_code` in `users` table
        # and you have added a uniq index on the column!!
        if User.find_by_confirmation_code(params[id])
          # success
        else
          # error
        end
      end
    end
    

    09-09 23:32
    查看更多