In Rails 3, how to redirect directly from routes when setting cookies

I just encountered a situation in the application in which I work, in which I wrote an action specifically for setting a language cookie and redirecting to the last URL. Something like that:

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

The fact is that I am not very happy with this. Since the goal of the endpoint is to install a new language for the server-side application, this ultra-light logical view gets stuck in the entire middleware package before it actually reaches the controller, which degrades workflow performance. But later I found out that I could redirect directly from the route cartographer (http://guides.rubyonrails.org/routing.html#redirection) and thought that I had found a solution to all my problems.

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

The problem is that although I have access to the request and therefore I can insert cookies into it, only the last value of the redirect unit will be accepted to form the redirect response. Which will not check the request and set its cookies (or whatever else may be there). So yes, the question is, does anyone know a way to influence the .rb route redirection a little more than by passing the destination route?

And no, I do not want a client-side solution. I fully know that I can do this with JS too, I just wanted a no-JS solution.

Thank you in advance

+4
source share
1 answer
 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 
0
source

All Articles