Rails 3 相当于controller.process(构建无缝用户身份验证)

发布于 2024-11-03 19:14:33 字数 587 浏览 1 评论 0原文

我一直在尝试开发用于用户身份验证的控制器操作,以便我可以执行以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2024-11-10 19:14:33

我在 Rails 3 中遇到了同样的问题,controller.process 已被弃用。
下面是我尝试使用 Rack::MethodOverride 代替controller.process(它在 Rails 2 中为我工作)的失败尝试。下面 ro.call 的错误是:

ThreadError in UserSessionsController#create
线程 0xb592b994 尝试加入自身

def demo

params = {'action' => 'create', 'controller' => 'user_sessions', 
        "commit"=>"Submit", "user_session"=>{"username"=>"demo", "password"=>"demo"}}

request.env['REQUEST_METHOD'] = 'POST'

# Throw out existing params and merge the stored ones
request.parameters.reject! { true }
request.parameters.merge!(params)

request.path_parameters.reject! { true }
request.path_parameters.merge!('action' => 'create', 'controller' => 'user_sessions')

request.POST["HTTP_X_HTTP_METHOD_OVERRIDE"] = 'POST'
ro = Rack::MethodOverride.new Rails.application
ro.call request.env
if response.redirected_to
  @performed_redirect = true
else
  @performed_render = true
end

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

params = {'action' => 'create', 'controller' => 'user_sessions', 
        "commit"=>"Submit", "user_session"=>{"username"=>"demo", "password"=>"demo"}}

request.env['REQUEST_METHOD'] = 'POST'

# Throw out existing params and merge the stored ones
request.parameters.reject! { true }
request.parameters.merge!(params)

request.path_parameters.reject! { true }
request.path_parameters.merge!('action' => 'create', 'controller' => 'user_sessions')

request.POST["HTTP_X_HTTP_METHOD_OVERRIDE"] = 'POST'
ro = Rack::MethodOverride.new Rails.application
ro.call request.env
if response.redirected_to
  @performed_redirect = true
else
  @performed_render = true
end

end

Might anyone know how to correctly use Rack::MethodOverride to send a post request?

始终不够 2024-11-10 19:14:33

您可以将该方法与用户重定向的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文