如何在Rails控制器中动态设置Cookie名称 [英] How to set cookie name dynamically in Rails controller

查看:71
本文介绍了如何在Rails控制器中动态设置Cookie名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails应用程序中动态设置cookie名称。

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

我能够这样设置cookie:

I am able to set cookie like this:

cookies[:users_id] = current_user.id



<我想动态设置cookie名称。我这样尝试过,但无法正常工作:

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

怎么可能?

推荐答案

您可以使用字符串也可以使用符号,并且如果要执行插值,则需要使用双勾号(

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

这篇关于如何在Rails控制器中动态设置Cookie名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆