簡體   English   中英

Rails,Heroku,Cloudinary:雖然在本地工作,但照片在生產中未上傳到 cloudinary

[英]Rails, Heroku, Cloudinary: Photos not uploading to cloudinary in production, although working locally

我已經為此撓頭一段時間了,我似乎找不到解決方案。 總而言之,我正在開發一個僅用於培訓的演示應用程序,有一次我會提示用戶上傳照片。 我在 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/ 這是我的 Model:

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/ 我的 controller:

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 上的生產中卻沒有? 肯定有我想念的東西。 一點幫助將不勝感激! 謝謝!

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

$ heroku addons:create cloudinary

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

Cloudinary 配置參數( cloud_nameapi_keyapi_secret )通過CLOUDINARY_URL配置變量定義。

確保;

配置/存儲.yml

cloudinary:服務:Cloudinary

配置/環境/development.rb

config.active_storage.service =:cloudinary

配置/環境/production.rb

config.active_storage.service =:cloudinary

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM