Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookies in ApplicationController?

I need to set cookies in my ApplicationController but I'm not sure how. I've tried using cookies - nothing, using ActionController::Cookies - nothing. I don't need anything more then setting and getting cookies but what I do need is to set them in ApplicationController.

EDIT:

Found the answer: request.cookies['help'] = 'yes'

like image 896
AndrewShmig Avatar asked Jun 05 '11 16:06

AndrewShmig


People also ask

How do I set cookies to all path?

In your Java server, you should call cookie. setPath("/") before adding it to response. Such cookie will match all request URIs.

Are cookies set automatically?

Cookies are usually set by a web-server using the response Set-Cookie HTTP-header. Then, the browser automatically adds them to (almost) every request to the same domain using the Cookie HTTP-header.


1 Answers

What do you mean by setting cookie in application controller? You would set cookie in browser corresponding to some controller action. If you want to set the cookie for all actions then you may consider using a before filter and apply that filter to all your controller actions.

You can set and delete cookies as shown below:

   cookies[:key] = {        :value => 'a yummy cookie',        :expires => 1.year.from_now,        :domain => 'domain.com'      }       cookies.delete(:key, :domain => 'domain.com') 

Please note that if you specify a :domain when setting a cookie, you must also specify the domain when deleting the cookie.

e.g. cookies[:user_name] = "david"

like image 96
amit_saxena Avatar answered Sep 20 '22 05:09

amit_saxena