创建产品时,我使用回形针宝石、回形针-dropbox宝石、菲加罗宝石和Dropbox上传和存储图像。在本地,在开发过程中,图像文件被很好地上传到数据库中,并且是可见的,但是在生产中,图像应该转到Dropbox,表单不会通过,当我查看Heroku日志时,我会得到一个Dropbox身份验证错误。我已经三次确认我的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:末尾的params
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 %>发布于 2014-07-13 22:30:10
在使用这种方法时,不要忘记告诉Heroku关于Figaro宝石的非常重要的一步。
rake figaro:heroku 为我做了个小把戏。看一下现在创业板,我没有看到这个命令,但是有一节专门介绍如何部署到Heroku。
发布于 2014-12-11 20:41:48
我也遇到了类似的问题,最后不得不在从开发到生产的条件下改变环境。
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当指定开发而不是生产时,我不知道为什么它不起作用,但至少后者是有效的。
发布于 2017-02-24 10:12:49
我可能迟了回答,但这可能对某人有帮助。
在Dropbox的开发人员控制台中,更改'Development user'设置可以工作。最初,当创建应用程序时,'Development users'将是'Only you'。
因此,单击“启用其他用户”按钮,允许更多用户访问该应用程序。
https://stackoverflow.com/questions/24715176
复制相似问题