【问题标题】:Rails, Heroku, Cloudinary: Photos not uploading to cloudinary in production, although working locallyRails、Heroku、Cloudinary:虽然在本地工作,但照片在生产中没有上传到 cloudinary
【发布时间】:2020-09-06 21:46:47
【问题描述】:

我一直在为此挠头,但似乎找不到解决方案。 总而言之,我正在开发一个仅用于培训的演示应用程序,有一次我会提示用户上传照片。 我在 rails 中使用 simple_form,并使用 cloudinary 来托管照片。在我的本地主机中一切正常,但应用程序在生产中中断,照片应该显示。原因是它们显然永远不会被上传,我找不到原因。 我将尝试分享与该问题相关的所有内容:

1/ 首先,我在 .env 文件中添加了我的 cloudinary 密钥:

CLOUDINARY_URL=cloudinary://298522699261255:Qa1ZfO4syfbOC-***********************8

2/ 我在 gemfile 中添加了以下 gem:

gem 'cloudinary', '~> 1.12.0'
gem 'dotenv-rails', groups: [:development, :test]

我看到这是在 gemfile.lock 文件中添加的:

    cloudinary (1.12.0)
      aws_cf_signer
      rest-client

3/ 在 .gitignore 文件中添加:

.env*

4/ 我在 config/environments/development.rb 和 production.rb 文件中添加了这个:

config.active_storage.service = :cloudinary

5/ 在 config/storage.yml 文件中添加这一行

cloudinary:
  service: Cloudinary

6/ 这是我的模特:

class Cocktail < ApplicationRecord
  has_many :doses, dependent: :destroy
  has_many :ingredients, through: :doses
  has_one_attached :photo

  validates :name, presence: true, uniqueness: true
end

7/ 还有我的控制器:

class CocktailsController < ApplicationController
  def index
    @cocktails = Cocktail.all
  end

  def show
    @cocktail = Cocktail.find(params[:id])
  end

  def new
    @cocktail = Cocktail.new
  end

  def create
    @cocktail = Cocktail.new(cocktail_params)
    if @cocktail.save
      redirect_to cocktail_path(@cocktail.id)
    else
      render :new
    end
  end

  private

  def cocktail_params
    params.require(:cocktail).permit(:name, :photo)
  end
end

8/ 这是我提示用户添加照片的 simple_form:

<h1>Create your Cocktail</h1>

<%= simple_form_for(@cocktail) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :name %>
  </div>
    <%= f.input :photo %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

<%= link_to "Back to Cocktails", cocktails_path %>

9/ 这是显示图片的地方(以及应用程序中断的地方,因为图片尚未上传到 Cloudinary)

<div class="container">
  <div class="row">
    <% @cocktails.each do |cocktail| %>
      <div class="col-4">
        <div class="container d-flex justify-content-between">
          <div class="card-trip">
              <%= cl_image_tag cocktail.photo.key, crop: :fill %>
            <div class="card-trip-infos">
              <div>
                <h2><%= link_to cocktail.name, cocktail_path(cocktail) %></h2>
              </div>
              <h2 class="card-trip-pricing"><%=  %></h2>
            </div>
          </div>
        </div>
      </div>
    <% end %>
  </div>
</div>

如果我忘记了什么,请告诉我,我会补充的。

此应用在本地运行良好。但是,一旦推送到 Heroku,原本可以正常工作的应用程序会在用户返回应该显示图片的索引页面时中断。可能的原因是图片没有上传到 Cloudinary,所以找不到它们。 所以我的问题是,为什么当我在本地测试应用程序时文件上传到 Cloudinary 而不是在 Heroku 上生产时?肯定有我想念的东西。 一点帮助将不胜感激! 谢谢!

【问题讨论】:

    标签: ruby-on-rails image heroku rubygems cloudinary


    【解决方案1】:

    您是否在 Heroku 上安装了插件?

    $ heroku addons:create cloudinary
    

    您是否向 Heroku 提供了 API 密钥和秘密? (配置变量)

    Cloudinary 配置参数(cloud_nameapi_keyapi_secret)通过CLOUDINARY_URL 配置变量定义。

    【讨论】:

    • 一定是这样,可惜我现在不能检查,但我会在一个小时左右。谢谢!
    • 感谢这解决了部分问题 :) 应用程序不再崩溃,但它也不会显示图片,因为由于某种原因它们仍然没有上传到 cloudinary。跨度>
    【解决方案2】:

    确定;

    config/storage.yml

    云: 服务:Cloudinary

    config/environments/development.rb

    config.active_storage.service = :cloudinary

    config/environments/production.rb

    config.active_storage.service = :cloudinary

    heroku 配置:设置 CLOUDINARY_URL=cloudinary://***************

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2020-07-18
      • 2018-01-29
      • 2019-04-18
      • 2020-03-22
      • 1970-01-01
      • 2015-11-01
      • 2019-05-07
      • 2021-12-08
      相关资源
      最近更新 更多