【问题标题】:Rails 4 - Paperclip - Dropbox production issueRails 4 - 回形针 - Dropbox 生产问题
【发布时间】:2014-09-03 02:36:45
【问题描述】:

我正在使用Paperclip gempaperclip-dropbox gemfigaro gem 和 Dropbox 在创建产品时上传和存储图像。在本地,在开发中,图像文件很好地上传到数据库,并且是可见的,但是在生产中,图像应该进入 Dropbox,表单没有通过,并且在查看时出现 Dropbox 身份验证错误在我的 Heroku 日志中。我已经三次检查了我的 Dropbox 安全密钥是否正确。我查看了所有相关问题,但找不到任何可行的方法。

这是heroku的错误:

 DropboxAuthError (User is not authenticated.):
2014-07-12T16:04:12.514637+00:00 app[web.1]:       app/controllers/products_controller.rb:31:in `block in create'
2014-07-12T16:04:12.514638+00:00 app[web.1]:       app/controllers/products_controller.rb:30:in `create'
2014-07-12T16:04:12.514640+00:00 app[web.1]: 
2014-07-12T16:04:12.514642+00:00 app[web.1]: 
2014-07-12T16:04:12.512474+00:00 app[web.1]: Completed 500 Internal Server Error in 2361ms

这是我的 products_controller #create 操作:

 # POST /products
 # POST /products.json
 def create
 @product = Product.new(product_params)
 @product.user = current_user

respond_to do |format|
  if @product.save
    format.html { 
      redirect_to @product,
       notice: 'Product was successfully created.'
        }
    format.json { 
      render json: @product,
       status: :created,
        location: @product 
      }
  else
    format.html { render :new }
    format.json { 
      render json: @product.errors,
       status: :unprocessable_entity
   }
   end
 end
end

这是 products_create 末尾的参数:

 private

 def set_product
  @product = Product.find(params[:id])
 end


 def product_params
  params.require(:product).permit(:description, :name, :permalink, :price, :file,  :user_id)
 end

这是我的产品型号:

 class Product < ActiveRecord::Base

 if Rails.env.development?
    has_attached_file :file
 else
    has_attached_file :file,
    :storage => :dropbox,
    :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
    :path => ":style/:id_:filename"
 end


 belongs_to :user
 has_many :sales

 validates_numericality_of :price,
    greater_than: 49,
    message: "must be at least 50 cents"

 validates_attachment_content_type :file, :content_type => %w(image/jpeg image/jpg     image/png)

end

最后是创建新产品的表格:

 <%= form_for(@product,:html => { :multipart => true }) do |f| %>
 <% if @product.errors.any? %>
 <div id="error_explanation">
  <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from  being saved:</h2>

  <ul>
  <% @product.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
  </div>
  <% end %>

  <div class="field">
  <%= f.label :name %><br>
  <%= f.text_field :name %>
   </div>

  <div class="field">
  <%= f.label :permalink %><br>
  <%= f.text_field :permalink %>
  </div>

  <div class="field">
  <%= f.label :description %><br>
  <%= f.text_area :description %>
  </div>

  <div class="field">
  <%= f.label :price %><br>
  <%= f.number_field :price %>
  </div>

  <div class="field">
  <%= f.label :user_id %><br>
  <%= f.text_field :user_id %>
  </div>

  <div class="field">
  <%= f.label :file %><br />
  <%= f.file_field :file %>
  </div>

  <div class="actions">
  <%= f.submit %>
  </div>

  <% end %>

【问题讨论】:

    标签: ruby-on-rails heroku ruby-on-rails-4 paperclip dropbox


    【解决方案1】:

    在使用这种方法时,不要忘记告诉 Heroku 关于 Figaro gem 的非常重要的一步。

     rake figaro:heroku 
    

    为我做了诀窍。查看gem page now,我没有看到那个命令,但是有一个部分专门用于部署到Heroku 来查看。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,最终不得不将条件中的环境从开发更改为生产。

      class Listing < ActiveRecord::Base
        if Rails.env.production?
          has_attached_file :image,
          :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg",
          :storage => :dropbox,
          :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
      :path => ":style/:id_:filename"
          validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png)
        else
          has_attached_file :image,
          :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
        end
      end
      

      我不知道为什么在指定开发而不是生产时它不起作用,但至少后者有效。

      【讨论】:

        【解决方案3】:

        我可能会迟到回答,但它可能会帮助某人。

        在 Dropbox 的开发者控制台中,更改 'Development user' 设置有效。 最初创建应用程序时,'Development users' 将是 'Only you'

        所以点击“启用其他用户”按钮,允许更多用户访问该应用程序。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-22
          • 2015-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-03
          • 1970-01-01
          相关资源
          最近更新 更多