Possiedo un'applicazione Rails 3.1 che utilizza la gemma paperclip (v 3.4.0). In poche parole. Ho un modello di storia e un modello di post. Una storia può avere molti post.Gemma Paperclip che attiva problemi di verifica del token 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
Come si può vedere, i messaggi possono avere una foto allegata. Uso la graffetta per gestire questi allegati.
Genero il modulo che POSTs questi post dinamicamente sul client con javascript/jquery. Il mio problema è questo . . Se il post NON include un allegato fotografico, tutto funziona perfettamente. SE, INVECE, UN POSTO HA UN foto allegata, ho visualizzato il seguente messaggio di errore e il post non fa 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
Di conseguenza, il mio dati della sessione viene distrutta, e non riesco nemmeno a vedere la Richiedi intestazioni con Firebug. La richiesta put semplicemente non appare in firebug.
Ora, non a caso, posso ottenere intorno a questo problema con il seguente nella PostController:
skip_before_filter :verify_authenticity_token, :only => [:create]
Ma io non voglio rinunciare a questa sicurezza. Ho anche provato ad aggiungere l'intestazione CSRF alla mia forma tramite js/jquery:
jQuery.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-
token"]').attr('content'));
}
});
Ma questo non risolve il problema, e come ho detto sopra, non riesco nemmeno a vedere i dati di richiesta di intestazione per vedere l'intestazione
Qualcuno può venire con un motivo per cui paperclip attiva il problema?
Stesso problema qui – Pasta