Rails 3 相当于controller.process(构建无缝用户身份验证)
我一直在尝试开发用于用户身份验证的控制器操作,以便我可以执行以下操作:
class PostsController < ApplicationController
before_filter :authenticate_user!
...
并在用户通过身份验证后重定向回上一页的身份验证操作。我知道有存储和重定向的方法可以解决这个问题。但是,这不适用于需要非 GET 操作的任何操作,即删除帖子。
我找到了 Rails 2 的解决方案,涉及使用 controller.process
方法从另一个控制器调用 POST 操作。不过,这在 Rails 3 中已被弃用。 Rails 3中有类似的方法吗?我找不到太多关于旧的 controller.process
命令的文档。
如果有人对另一种方法有建议,我们也将不胜感激。
I've been trying to develop a controller action for user authentication so I can do something like this:
class PostsController < ApplicationController
before_filter :authenticate_user!
...
and have an authentication action that redirects back to the previous page after the user is authenticated. I'm aware that there are store-and-redirect approaches to solve this problem. However, this doesn't work for anything that requires a non-GET action, i.e. deleting a post.
I've found a solution for Rails 2 that involves using the controller.process
method to call a POST action from another controller. This is deprecated in Rails 3, though. Is there some equivalent method in Rails 3? I can't find much documentation on the old controller.process
command to begin with.
If someone has a suggestion for another approach, that would be appreciated as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Rails 3 中遇到了同样的问题,controller.process 已被弃用。
下面是我尝试使用 Rack::MethodOverride 代替controller.process(它在 Rails 2 中为我工作)的失败尝试。下面 ro.call 的错误是:
ThreadError in UserSessionsController#create
线程 0xb592b994 尝试加入自身
def demo
end
有人知道如何正确使用 Rack::MethodOverride 发送发布请求吗?
I have the same problem in Rails 3 with controller.process being deprecated.
Below is my failed attempt to use Rack::MethodOverride in place of controller.process (which was working for me in Rails 2). The error from ro.call below is:
ThreadError in User sessionsController#create
thread 0xb592b994 tried to join itself
def demo
end
Might anyone know how to correctly use Rack::MethodOverride to send a post request?
您可以将该方法与用户重定向的 url 一起存储在会话中,然后稍后伪造它。不过,您必须在机架中间件级别上执行此操作。例如,检查 lib/rack/methodoverride.rb 中的 Rack::MethodOverride 。
You can store the method together with the url the user was redirected from in the session, and then fake it later. You'll have to do it on the Rack middleware level though. Check Rack::MethodOverride in lib/rack/methodoverride.rb for example.