我有一个Rails 3.1应用程序,使用回形针宝石(v 3.4.0)。简而言之。我有一个故事模型和一个后期模型。一个故事可以有很多帖子。回形针宝石触发CSRF令牌验证问题
#story.rb
class Story < ActiveRecord::Base
attr_accessible :title, :user_id, :username, :posts_attributes
belongs_to :user
has_many :posts, :dependent => :destroy,
:order => "created_at DESC"
accepts_nested_attributes_for :posts, :reject_if => lambda { |t| t['contents'].nil? }
end
#post.rb
class Post < ActiveRecord::Base
attr_accessible :contents, :photo, :dimensions
belongs_to :story, :touch => true
belongs_to :user, :touch => true
has_attached_file :photo,
:styles => {
:medium => { :geometry => "400x400>" },
:thumb => { :geometry => "100x100>" },
},
:processors => [:thumbnail],
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/:style/:id/:filename"
before_save :extract_dimensions
serialize :dimensions
validates :contents, :presence => true,
:length => { :maximum => 399,
:minimum => 5 }
validates :user_id, :presence => true
validates_attachment_content_type :photo,
:content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'],
:message => "Sorry, we don't support that type of image format"
end
正如你所看到的信息可能有照片附件。我使用回形针来管理这些附件。
我使用javascript/jquery生成在客户端动态发布这些帖子的表单。我的问题是这样的。 。 。如果该帖子不包含照片附件,那么所有内容都可以正常工作但是,如果一个帖子有照片附件,我收到以下错误消息和帖子不POST:
WARNING: Can't verify CSRF token authenticity
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 61 LIMIT 1
(0.3ms) BEGIN
(0.2ms) COMMIT
Completed 401 Unauthorized in 238ms
其结果是,我的会话数据被破坏,我甚至不能看请求标题与萤火虫。放入请求根本不会出现在萤火虫中。
现在,这并不奇怪,我可以解决这个问题,在PostController中的以下内容:
skip_before_filter :verify_authenticity_token, :only => [:create]
但我不想放弃这个安全性。我也试图通过添加CSRF头到我的形式JS/jQuery的:
jQuery.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-
token"]').attr('content'));
}
});
但是,这并不解决问题,正如我上面所说的,我甚至不能看到请求头数据看标题。
任何人都可以提出一个为什么回形针触发问题的原因吗?
同样的问题在这里 – Pasta 2013-04-14 21:37:00