Rails,Heroku,Cloudinary:尽管在本地工作,但照片并未上传到生产中的cloudinary中

问题描述 投票:0回答:1

我已经为此花了一段时间,但我似乎找不到解决方法。综上所述,我正在开发一个演示应用程序,仅用于培训,在这一点上,我提示用户上传照片。我在rails和cloudinary中使用simple_form托管照片。在我的本地主机上一切正常,但是该应用程序停止生产,应尽快显示照片。这样做的原因是,它们显然永远都不会上传,而我找不到原因。我将尝试分享与该问题相关的所有内容:

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

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

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

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上进行生产时却上传到Cloudinary?肯定有我缺少的东西。一点帮助将不胜感激!谢谢!

ruby-on-rails image heroku rubygems cloudinary
1个回答
0
投票

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

$ heroku addons:create cloudinary

您是否为heroku提供了api密钥和秘密? (配置变量)

© www.soinside.com 2019 - 2024. All rights reserved.