4

I've just come across a situation in the app I'm working in, in which I wrote an action specifically for setting the language cookie and redirect to the last url. Something like this:

def language
  cookies[:locale] = {:value => params[:locale], :domain => APP_DOMAIN}
  redirect_to_back
end

Thing is, I'm not very pleased with this. Since the whole purpose of the endpoint is to set a new language for the app on the server side, this super-light logic kind of gets jammed in the whole middleware stack before it actually reaches the controller, which hurts the performance of the workflow. But later, I found out I could redirect straight from the route mapper (http://guides.rubyonrails.org/routing.html#redirection), and thought I had found the solution to all my problems.

match "language/:locale" => redirect {|p, req| req.cookies["locale"] = {:value => p[:locale], :domain => APP_DOMAIN} ; req.referrer || '' }

Problem is, Even though I have access to the request, and can therefore insert cookies in it, only the last value of the redirect block will be taken to form the redirection response. Which will not check the request and set its cookies (or whatever more there may be) accordingly. So yeah, the question is, does anybody know a way of influencing the route.rb redirections a bit more than by passing the destination route?

And no, I don't want a client-side solution. I'm fully aware I can also do this with JS, I just wanted a no-JS solution.

Thanks in advance

4

1 回答 1

0
Movienight::Application.routes.draw do
  match '/movies/search', to: proc { |env|
    [
      302,
      {"Content-Type" => 'text/plain',
       'Location' => 'cosmicvent.com',
       'Set-Cookie' => 'sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT'},
       ['302 found']
    ]
 }
end
于 2015-04-20T10:50:41.523 回答