关于session存储的问题

本文介绍了解决 Rails 应用中遇到的 ActionController::InvalidAuthenticityToken 错误的方法。通过设置 protect_from_forgery 的 secret 参数,可以避免伪造请求导致的安全问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium]ActionController::InvalidAuthenticityToken解决方法[/size]

Rails支持将session放在数据库中管理,当我执行完rake命令,去掉environment.rb里的config.action_controller.session_store这个注释,Rails2.0以上的版本会报一个错,具体如下:
ActionController::InvalidAuthenticityToken in Products#index
Showing products/index.html.erb where line #17 raised:

No :secret given to the #protect_from_forgery call. Set that or use a session store capable of generating its own keys (Cookie Session Store).
于是上网搜搜,解决方法是到这个文件app/controllers/application.rb,看到这样的描述:
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery #:secret => 'be73f1c53c4d8c3b373da4ebfb6599a2'
于是,把#:secret => 'be73f1c53c4d8c3b373da4ebfb6599a2'前的#去掉,再刷新页面,OK。问题解决,顺便记下来备忘。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值