Rails 3 equivalent for controller.process (building seamless user authentication)
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.
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?
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.
精彩评论