如何将定制的RESTful路由添加到Rails应用程序? [英] How to add a custom RESTful route to a Rails app?

查看:62
本文介绍了如何将定制的RESTful路由添加到Rails应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这两页


  1. 资源

  2. 添加更多RESTful行为

  1. resources
  2. Adding more RESTful actions

Rails指南页面显示

The Rails Guides page shows

map.resources :photos, :new => { :upload => :post }

及其相应的URL

/photos/upload

这看起来很棒。

我的 routes.rb 显示此

map.resources :users, :new => { :signup => :get, :register => :post }

当我这样做时: [〜/ my_app] $耙路

我看到增加了两条新路线

I see the two new routes added

  signup_new_user GET    /users/new/signup(.:format)
register_new_user POST   /users/new/register(.:format)

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

Note the inclusion of /new! I don't want that. I just want /users/signup and /users/register (as described in the Rails Routing Guide).

有帮助吗?

推荐答案

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

When you expose a controller as a resource, following actions are automatically added:

show
index
new
create
edit
update
destroy

这些动作可以分为两类:

These actions can be categorized in to two groups:


  • :成员操作

  • :member actions

成员操作的URL具有目标资源的ID。例如:

The URL for the member action has the id of the target resource. E.g:

users/1/edit 
users/1

您可以将:member 动作视为类的实例方法。

You can think of :member action as an instance method on a class. It always applies on an existing resource.

默认成员操作: show edit 更新销毁

Default member actions: show, edit, update, destroy


  • :collection 操作

  • :collection actions

URL :collection 操作不包含目标资源的ID。例如:

The URL for the :collection action does not contain the id of the target resource. E.g:

users/login
users/register

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

You can think of :collection action as a static method on a class.

默认收集操作:索引 create

在您的情况下,您需要执行两个新的注册操作。这些动作属于:collection类型(因为提交这些动作时您没有用户的ID)。您的路线可以如下所示:

In your case you need two new actions for registration. These actions belong to :collection type( as you do not have the id of the user while submitting these actions). Your route can be as follows:

map.resources :users, :collection => { :signup => :get, :register => :post }

操作的网址如下:

users/signup
users/register

如果要删除由Rails生成的标准操作,请使用:except /:only选项:

If you want to remove a standard action generated by Rails use :except/:only options:

map.resources :foo, :only => :show

map.resources :foo, :except => [:destroy, :show]

编辑1

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

I usually treat the confirmation action as a :member action. In this case params[id] will contain the confirmation code.

路由配置:

map.resources :users, :member => { :confirm => :get}

URL

/users/xab3454a/confirm

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

控制器

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

这篇关于如何将定制的RESTful路由添加到Rails应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆