Ruby on Rails:自定义操作 [英] Ruby on Rails: Custom actions

查看:42
本文介绍了Ruby on Rails:自定义操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 新手,所以我将向您解释我的情况:

I'm new to rails, so I'll just explain my situation to you:

我有一个用户模型和一个用户控制器.用户使用他们的电子邮件地址和密码登录.特殊用户可以通过在表单中​​输入被邀请者的电子邮件地址并点击提交来邀请其他用户.受邀用户随后会收到一封邮件,其中包含用于激活其帐户的链接,只需第一次输入其密码即可.

I've got a User Model and a UsersController. Users log in with their email address and a password. Special users can invite other users, by typing the email address of the invitee into a form and hitting submit. The invited user then receives a mail containing a link to activate his account, by entering his password for the first time.

问题来了:

邀请"表单映射到我的 UsersController atm 的创建操作.但是我将激活"表单映射到什么?

The "invitation" form is mapped to the create action of my UsersController atm. But what do I map the "activation" form to?

我是否可以定义自定义操作激活"或诸如 /users/3/activate 之类的可以访问的操作(当然,这里也应该有一些身份验证令牌...) 并会激活 ID 为 3 的用户?

Is it possible for me to define a custom action "activate" or something that can be accessed like /users/3/activate (of course, there should be some authentication token here too...) and would activate the user with id 3?

我发现了一些关于自定义操作的内容,但我还没有完全掌握.

I've found some stuff on custom actions, but I don't quite get the hang of it yet.

感谢任何帮助

推荐答案

您的路由文件中可能有这样的内容.(如果没有,请发布.)

You probably have something like this in your routes file. (If not, please post that.)

resources :users 

如果您想添加一个作用于单个 User 的自定义操作,您可以像这样将其添加为 :member.

If you want to add a custom action that acts on a single User, you can add that as a :member like this.

resources :users do
  member do
    get :activate
  end
end

请注意,对修改数据的内容使用 get 有点错误,但您是在谈论从电子邮件中的链接点击它.

Note that using a get for something that modifies data is a bit wrong, but you're talking about hitting this from a link in an email.

在玩路由时不要忘记 rake routes 会显示你当前可用的所有路由.

As you play with routes don't forget that rake routes will show you all of the routes that you currently have available to you.

这篇关于Ruby on Rails:自定义操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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