我使用的rails版本是 3.0.5 的 ,然后使用的是 cookie_session ,现在遇到一个问题:
当我使用admin admin 登录之后,打开其他页面访问session的时候都没有问题,可以访问的到
http://localhost:3000/users/1/roles 这种url也可以访问的到session
但是当我访问以下url的时候
http://localhost:3000/users/1/roles/1
session 就清空了,我访问不到session中的值了,session中,原来就存了一个session[:user], 不知道是怎么回事
我的路由设置是
resources :users, :member => { :enable => :put } do
resources :roles
end
模仿的是Ruby On Rails 社区网站开发 编写的代码。
后来发现是 所有不是get 请求的都是清空session, put delete 都不行。
由此就奇怪了,然后我就想到要看一下 这个清空session函数 reset_session方法是什么时候有执行过,最后在 rails的gems 包中找到了如下代码:
C:\Ruby187\lib\ruby\gems\1.8\gems\actionpack-3.0.5\lib\action_controller\metal\request_forgery_protection.rb
如此便是找到了清空session的罪魁祸首,是因为 rails 的跨域访问问题的解决,所以导致的这个问题, 在rails进行before_filter 之前,要进行 protect_from_forgery 的校验,凡是不是 get请求的,或者是html、javascript请求的都直接清空session,而且不给予你任何提示。 这就是最恶心的地方,3.0.1的时候还是抛异常,如下代码:
因此我找了有一天时间才确认是这个问题
我犹豫不需要跨域访问,所以我采取了最简单的方法,则是把application_controller.rb中的内容 protect_from_forgery 删除掉就可以了,或者使用 skip_before_filter :verify_authenticity_token (还没有试验过),至于以后的跨域访问研究,以后再更新。。。
还要上班呢。
当我使用admin admin 登录之后,打开其他页面访问session的时候都没有问题,可以访问的到
http://localhost:3000/users/1/roles 这种url也可以访问的到session
但是当我访问以下url的时候
http://localhost:3000/users/1/roles/1
session 就清空了,我访问不到session中的值了,session中,原来就存了一个session[:user], 不知道是怎么回事
我的路由设置是
resources :users, :member => { :enable => :put } do
resources :roles
end
模仿的是Ruby On Rails 社区网站开发 编写的代码。
后来发现是 所有不是get 请求的都是清空session, put delete 都不行。
由此就奇怪了,然后我就想到要看一下 这个清空session函数 reset_session方法是什么时候有执行过,最后在 rails的gems 包中找到了如下代码:
C:\Ruby187\lib\ruby\gems\1.8\gems\actionpack-3.0.5\lib\action_controller\metal\request_forgery_protection.rb
# Turn on request forgery protection. Bear in mind that only non-GET, HTML/JavaScript requests are checked. # # Example: # # class FooController < ApplicationController # protect_from_forgery :except => :index # # You can disable csrf protection on controller-by-controller basis: # # skip_before_filter :verify_authenticity_token # # It can also be disabled for specific controller actions: # # skip_before_filter :verify_authenticity_token, :except => [:create] # # Valid Options: # # * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified. def protect_from_forgery(options = {}) self.request_forgery_protection_token ||= :authenticity_token prepend_before_filter :verify_authenticity_token, options end end protected # The actual before_filter that is used. Modify this to change how you handle unverified requests. def verify_authenticity_token verified_request? || handle_unverified_request end def handle_unverified_request reset_session end
如此便是找到了清空session的罪魁祸首,是因为 rails 的跨域访问问题的解决,所以导致的这个问题, 在rails进行before_filter 之前,要进行 protect_from_forgery 的校验,凡是不是 get请求的,或者是html、javascript请求的都直接清空session,而且不给予你任何提示。 这就是最恶心的地方,3.0.1的时候还是抛异常,如下代码:
# Turn on request forgery protection. Bear in mind that only non-GET, HTML/JavaScript requests are checked. # # Example: # # class FooController < ApplicationController # protect_from_forgery :except => :index # # # you can disable csrf protection on controller-by-controller basis: # skip_before_filter :verify_authenticity_token # end # # Valid Options: # # * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified. def protect_from_forgery(options = {}) self.request_forgery_protection_token ||= :authenticity_token before_filter :verify_authenticity_token, options end end protected def protect_from_forgery(options = {}) self.request_forgery_protection_token ||= :authenticity_token before_filter :verify_authenticity_token, options end # The actual before_filter that is used. Modify this to change how you handle unverified requests. def verify_authenticity_token verified_request? || raise(ActionController::InvalidAuthenticityToken) end # Returns true or false if a request is verified. Checks: # # * is the format restricted? By default, only HTML requests are checked. # * is it a GET request? Gets should be safe and idempotent # * Does the form_authenticity_token match the given token value from the params? def verified_request? !protect_against_forgery? || request.forgery_whitelisted? || form_authenticity_token == params[request_forgery_protection_token] end # Sets the token value for the current session. def form_authenticity_token session[:_csrf_token] ||= ActiveSupport::SecureRandom.base64(32) end
因此我找了有一天时间才确认是这个问题
我犹豫不需要跨域访问,所以我采取了最简单的方法,则是把application_controller.rb中的内容 protect_from_forgery 删除掉就可以了,或者使用 skip_before_filter :verify_authenticity_token (还没有试验过),至于以后的跨域访问研究,以后再更新。。。
还要上班呢。
发表评论
-
mac系统装rvm 和 1.9.3
2014-04-02 21:00 0rvm install 1.9.3 --with-gcc ... -
ruby on rails ubuntu 数据库安装 mysql postgresql
2012-10-05 17:33 761ubuntu mysql 安装 sudo apt-get i ... -
Linecache19 fails to compile with 1.9.3-head
2012-07-29 23:55 924Linecache19 fails to compile wi ... -
ubuntu 安装 sqlite3 gem包时报错
2012-07-29 15:57 779ubuntu 安装 sqlite3 gem包时报错 Buil ... -
rmagick gem包 兼容windows xp 7
2012-04-16 16:55 554rmagick 2.12.0 gem包 兼容windows x ... -
ActionMailer: Hostname not match server certificate
2011-05-26 17:24 1388ActionMailer: Hostname not matc ... -
Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first. 23s
2011-05-26 17:17 4448在配置发送邮件设置的时候 ActionMailer::Ba ... -
国外部署网站,time_zone 时区设置
2011-04-14 14:06 0最近研究了一下 rails的时区设置,主要是在 rails A ... -
rake aborted! Mysql::Error: query: not connected: CREATE TABLE `schema_migration
2011-03-14 18:21 1408rake aborted! Mysql::Error: que ... -
rails3 ActionController::RoutingError (uninitialized constant ApplicationControl
2011-03-10 09:52 1670rails3 ActionController::Routin ... -
ERROR NoMethodError: private method `gsub' called for
2011-01-21 11:25 1257ERROR NoMethodError: private me ... -
rails 安装数据库mysql sqlite 包时报错 解决
2011-01-04 22:15 1168rails在gem install mysql/sqlite3 ... -
rubymine 3.0 4.0 注册码 序列号 破解
2011-01-04 15:51 2766name: rubymine ===== LICENSE BE ... -
Called id for nil, which would mistakenly be 4 错误解决之一
2010-12-24 16:17 1112Called id for nil, which would ... -
will_paginate 与 rails3集成时,分页 界面显示不出来
2010-11-18 19:59 1537rails 3新增XXS机制,导致html标签在output的 ...
相关推荐
Rails :: SessionCookie 快速,松散耦合的请求有关经过Cookie验证的应用程序的规范。 为什么 可能,您可能已经看到了很多像这样的代码: # config/initializers/session_store.rb Rails . application . config . ...
activerecord-session_store, 从 Rails 中提取的记录存储的活动会话 Active Record 会话存储由 Active Record 类支持的会话存储。 提供了默认类,但是任何对 Active Record 会话类的对象鸭类型都有文本 session_id ...
rails generate active_record:session_migration 运行迁移: rake db:migrate 然后,在config/initializers/session_store.rb设置会话存储: Rails . application . config . session_store :active_record_...
Ajax、Prototype和Scriptaculous等JavaScript代码库和RJS,Session管理、用户登录和认证系统,XML和ActiveResource,后台处理和ActionMaile,测试和specs(包括RSpec on Rails和Selenium),安装、管理、编写插件,...
本人学习总结的ruby on rails 3.1.0数据库类查询方法,比较全了
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
If you’re new to Rails, you’ll get step-by-step guidance. If you’re an experienced developer, this book will give you the comprehensive, insider information you need., Rails has evolved over the ...
If you’re a web developer or designer ready to learn Ruby on Rails, this hands-on guide is the ideal way to get started. Rather than toss you into the middle of the framework’s Model-View-Controller...
不敢说是最好的。但绝对是很有用的。 和网上很多文章不一样。很实用。网上的一些文章都转来转去的。 刚开始还会让你越看越晕。这个是我的亲身的体验!所以我把最实用的上传上来,还希望大家...尤其是rails2.0的。
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
Rails 4 introduces a number of user-facing changes, and the ebook has been updated to match all the latest changes and new best practices in Rails. This includes full support for Ruby 2.0, controller ...
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
使用Rails构建可伸缩和可维护API的最佳方法
Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) Rails is large, powerful, and new. How do you use it effectively? How do you harness the power? And, ...