问题描述
|
在我的网络应用程序中,我通过电子邮件进行了典型的用户激活过程。
目前,对于激活过程中的每个步骤,我都有一个自定义的控制器动作,即,我有一个动作“ activate”,或多或少地简单地渲染了“ register”页面,然后还有另一个动作\“激活\”是注册页面上实际表单的目标操作。
我想知道这是否是最佳做法吗? -现在,我正处于实施管理员启动的密码重置的边缘(管理员单击特定用户的密码重置链接,该用户会收到一封电子邮件,其中包含指向该页面的链接,他可以在其中设置新密码) )。我会走得更远,再添加3个控制器动作(一个用于发送重置电子邮件,一个用于访问重置页面的用户,另一个用于实际重置表单的动作)。
这似乎使我的控制器有些混乱,我想知道这是否是“正确”的方法?
谢谢任何建议
解决方法
您正在执行的操作很好-无需使控制器操作符合RESTful操作。 REST仅应在适合您的模型的情况下使用,但您不应尝试使模型符合RESTful架构,除非它使交互更容易且直观。
最好将这些自定义操作放到自己的控制器中,而不是在UsersController中。一旦感觉到您的控制器变得过于拥挤,将一些动作移到各自的模块或控制器中可能是个好主意。
class RegistrationController
def activate # perform the activation
..
end
def activation # show the activation page
...
end
end
class PasswordController
def send # send the email
end
def resetter # show the page to reset the password
end
def reset # actually reset the password
end
end
可以通过自定义路由而不是资源来访问这些控制器操作。
match \'register/activation\' => \'registration#activation\'
post \'register/activate\' => \'registration#activate\'
post \'password/send\' => \'password#send\'
match \'password/resetter\' => \'password#resetter\'
post \'password/reset\' => \'password#reset\'