1

i want to store user selected location as cookie. if location is not selected automatically it should fetch users browsing location and store as a cookie value. How to do so?

i have done the code like this when user selects a location, location is storing as cookie now by this code.

@location = Location.find(location).name if location.present? cookies[:location_id] = { :value => @location, :expires => Time.now + 3600}

But i don't have any idea to fetch users browsing location automatically and store in cookie.

Please help me to do this.

Any help is appreciatable.

SreRoR
  • 1,151
  • 1
  • 17
  • 44

1 Answers1

0

I have referred to the following for retrieving current url:

How do I get the current absolute URL in Ruby on Rails?

For references on how to store cookie, the references can be found from the following:

(Do note the differences in different type of cookies that you can set, as you might want to set a cookie that expires on browser close or cookie that do not expires)

http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html

Combining both of these, I can write the code as follows:

Storing cookie:

cookies[:url] = request.original_url

Retrieving stored cookie:

url = cookies[:url]

Hope this helps.

Do let me know if I didn't answer your question correctly or you have any other questions.

Community
  • 1
  • 1
yihyang
  • 111
  • 1
  • 5