0

I am trying to set cookie name dynamically in my rails application.

I am able to set cookie like this:

cookies[:users_id] = current_user.id

I want to set the cookie name dynamically. I tried like this and it's not working:

cookies[:'users_id_#{current_user.id}'] = current_user.id

How is it possible?

webster
  • 3,902
  • 6
  • 37
  • 59
  • What do you want to achieve on higher level with this code, BTW? – Marek Lipka Apr 30 '15 at 09:24
  • 1
    Actually I don't want to set the cookie for user_ids. I just took an example for asking the question. The actual implementation is on an Article rating system, where an Article cannot be rated multiple times. – webster Apr 30 '15 at 09:42

1 Answers1

2

You can use string as well as symbol, and if you want to perform interpolation, you need to use double ticks (")

cookies["users_id_#{current_user.id}"] = current_user.id
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91