Paperclip gem在Rails中崩溃了用户的会话

时间:2013-05-15 04:11:03

标签: ruby-on-rails paperclip

我发现了类似的问题,但我的情况有点不同 (Paperclip 3.4.1,Rails 3.2.12,Ruby 1.9.3p392,使用Pow 0.40)

这是我的模特:

class Smoke < ActiveRecord::Base

  [...]

  attr_accessible :latitude, :longitude, :description, :picture

  validates_length_of :description, :minimum => 2, :maximum => 256, :allow_blank => true
  validates_presence_of :latitude, :longitude

  scope :with_picture, where("picture_file_name IS NOT NULL")

  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :smoke_reports, :dependent => :destroy

  has_attached_file :picture,
    :default_url => "/assets/missing/:style.jpg",
    :styles => { :thumb => ["55x55", :jpg], :medium => ["500x500", :jpg], :original => ["800x800", :jpg] },
    :storage => :s3,
    :bucket => 'my-bucket-dev',
    :path => "/:class/:style/:id.:extension",
    :url => ":s3_domain_url",
    :s3_credentials => {
      :access_key_id => 'mys3credentials',
      :secret_access_key => 'mys3credentials'
  }

  validates_attachment_size :picture,
    :less_than => 2.megabytes

  validates_attachment_content_type :picture,
    :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png)$/,
    :message => "Invalid image type. Only jpg/jpeg or png will be accepted."

  [...]

end

在哪里'[...]'我省略了不相关的东西。

当validates_attachment_size或validates_attachment_content_type失败时,会发生一些错误并清除我的所有会话数据(甚至我的登录)。

这就是应用程序日志中出现的所有内容,请检查它:

Started POST "/smokes" for 127.0.0.1 at 2013-05-14 23:48:38 -0300
Processing by SmokesController#create as HTML
WARNING: Can't verify CSRF token authenticity
Redirected to http://weedmap.dev/login
Filter chain halted as :deny_unlogged_users rendered or redirected
Completed 302 Found in 9ms (ActiveRecord: 2.4ms)

以下是我上传的文件通过这两种验证方法时日志显示的内容。因此,只有在文件未验证时才会出现此错误。检查:

Started POST "/smokes" for 127.0.0.1 at 2013-05-14 23:33:33 -0300
Processing by SmokesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DU+9CCVAzIxBbeccCWR1z4IzfQHxpiht6xPVCS/q3tc=", "latitude"=>"58.81374171570781", "longitude"=>"77.87109375", "picture"=>#<ActionDispatch::Http::UploadedFile:0x007f9f9c17e330 @original_filename="Untitled-1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture\"; filename=\"Untitled-1.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/RackMultipart20130514-59252-1xbz3zf>>, "description"=>"", "commit"=>"Create"}
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an.jpg[0]'
Command :: convert '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an.jpg[0]' -auto-orient -resize "800x800" '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c43.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don.jpg[0]'
Command :: convert '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don.jpg[0]' -auto-orient -resize "55x55" '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don20130514-59252-4r2p3r.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don.jpg[0]'
Command :: convert '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don.jpg[0]' -auto-orient -resize "500x500" '/var/folders/6n/2zzqcbfd1c51xz1phh8sz17h0000gn/T/Untitled-120130514-59252-bu86an20130514-59252-l87c4320130514-59252-km9don20130514-59252-1byzja6.jpg'
   (0.2ms)  BEGIN
  SQL (1.1ms)  INSERT INTO "smokes" ("created_at", "description", "latitude", "longitude", "picture_content_type", "picture_file_name", "picture_file_size", "picture_updated_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"  [["created_at", Wed, 15 May 2013 02:33:33 UTC +00:00], ["description", ""], ["latitude", 58.81374171570781], ["longitude", 77.87109375], ["picture_content_type", "image/jpeg"], ["picture_file_name", "Untitled-1.jpg"], ["picture_file_size", 8623], ["picture_updated_at", Wed, 15 May 2013 02:33:33 UTC +00:00], ["updated_at", Wed, 15 May 2013 02:33:33 UTC +00:00], ["user_id", nil]]
  SQL (0.7ms)  INSERT INTO "versions" ("created_at", "event", "ip", "item_id", "item_type", "object", "whodunnit") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["created_at", Wed, 15 May 2013 02:33:33 UTC +00:00], ["event", "create"], ["ip", "127.0.0.1"], ["item_id", 17], ["item_type", "Smoke"], ["object", nil], ["whodunnit", #<User id: 1, name: "Fernando Schuindt", email: "f.schuindtcs@gmail.com", password_digest: nil, created_at: "2013-05-07 23:23:42", updated_at: "2013-05-10 06:18:41", external_provider: true, avatar_setup: 3>]]
[paperclip] Saving attachments.
[paperclip] saving /smokes/original/17.jpg
[AWS S3 200 2.147466 0 retries] put_object(:acl=>:public_read,:bucket_name=>"weedsquare-dev",:content_length=>8623,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: Untitled-120130514-59252-bu86an20130514-59252-l87c43.jpg,:key=>"smokes/original/17.jpg") 

我很丢失。 有什么帮助吗?

更新

这是我的SmokesController的一部分:

class SmokesController < ApplicationController

  before_filter :deny_unlogged_users, :except => [:new, :show, :count, :refresh_profile]
  before_filter :only_ajax, :only => [:new, :count, :refresh_profile]
  before_filter :only_with_smoke_params, :only => [:create]

  [...]

  def create
    @user = current_user
    @smoke = Smoke.new
    @smoke.latitude = params[:latitude]
    @smoke.longitude = params[:longitude]
    @smoke.picture = params[:picture]
    @smoke.description = params[:description]
    if @smoke.save
      @user.smokes << @smoke
      redirect_to @smoke
    else
      render 'new'
    end
  end

  [...]

end

更新2:

在#rubyonrails@freenode.net上谈论这个问题,每个人都问我关于我的表格。 然后我意识到我正在手动设置authenticity_token,因为你可以看到here。现在form_tag正在自动执行。 (Rails默认做,但我不知道)

无论如何,这是我的表格,仍然是同样的错误,同样的事情:

  <div id="new-smoke-form">
    <%= form_tag '/smokes', :multipart => true, :method => "post" do %>
      <%= hidden_field_tag 'latitude', @smoke.latitude %>
      <%= hidden_field_tag 'longitude', @smoke.longitude %>
      <div id="new-smoke-picture">
        <p>Upload a picture (optional):</p>
        <%= file_field_tag 'picture' %>
        <div class="clear">
        </div>
      </div>
      <div id="new-smoke-description">
        <p>Talk about it:</p>
        <div id="new-smoke-remain" class="new-smoke-remain">256</div>
        <%= text_area_tag 'description', nil, :class => "new-smoke-textarea", :id => "new-smoke-textarea" %>
      </div>
      <%= submit_tag "Create", :class => "new-smoke-button" %>
    <% end %>
  </div>

我生气了。

1 个答案:

答案 0 :(得分:4)

我今天遇到过这个问题,看起来这与Pow而不是Paperclip存在问题,与此相关:https://github.com/37signals/pow/issues/125

我只是通过使用Pow作为WEBrick的端口代理而不是作为Rack应用程序来实现它的工作,希望这对你也有帮助。