webster
webster

Reputation: 4032

How to set cookie name dynamically in Rails controller

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?

Upvotes: 0

Views: 544

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51191

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

Upvotes: 2

Related Questions