簡體   English   中英

Rails、Active Record、Devise 和 cloudinary、簡單表格、heroku……照片沒有上傳到我的 cloudinary

[英]Rails, Active Record, Devise and cloudinary, simple form, heroku… photo does not upload to my cloudinary

好的,你在標題中得到了我的問題的要點。 我在 Rails 中創建了一個應用程序,它要求用戶創建個人資料並上傳圖片才能訪問某些功能。 無論如何,除了圖片上傳部分,一切正常。

我對這一切都很陌生,特別是對 Devise 有點困惑。 我正在尋找用戶 controller,我應該在哪里找到用戶的創建操作,對嗎? 但我不知道在哪里可以找到它。

話雖如此,我什至不確定問題是否來自 controller。 問題的根源可能是簡單的形式嗎? 不知道。 我已經搜索和嘗試了很長一段時間,我認為是時候詢問社區了:)

我將分享我能想到的與問題有關的所有內容。 如果您需要我展示其他內容,請告訴我。

1/ 用戶 model:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :user_instruments, dependent: :destroy
  has_many :instruments, through: :user_instruments
  has_many :messages, dependent: :destroy
  has_one_attached :photo, dependent: :destroy
  has_many :reviews_written, class_name: "Review", foreign_key: :writer_id
  has_many :reviews_received, class_name: "Review", foreign_key: :receiver_id
  has_many :jam_sessions, dependent: :destroy

  def profile_picture
    if photo.attached?
      photo.key
    else
      "avatar-unknown.png"
    end
  end

  def full_name
    "#{first_name} #{last_name}"
  end
end

2/ 應用 controller:

class ApplicationController < ActionController::Base
  before_action :authenticate_user!, except: [:home, :index, :show]

  before_action :configure_permitted_parameters, if: :devise_controller?
  def configure_permitted_parameters
    # For additional fields in app/views/devise/registrations/new.html.erb
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :bio])
    # For additional in app/views/devise/registrations/edit.html.erb
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end

  def default_url_options
    { host: ENV["DOMAIN"] || "localhost:3000" }
  end

end

3/ 簡單表單所在的新視圖:

<div class="container h-100">
  <div class="row justify-content-center align-items-center mt-3">
    <div class="col col-5">

      <h2>Sign up</h2>



      <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= f.error_notification %>

        <div class="form-inputs">
          <%= f.input :email,
                      required: true,
                      autofocus: true,
                      input_html: { autocomplete: "email" }%>
          <%= f.input :password,
                      required: true,
                      hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                      input_html: { autocomplete: "new-password" } %>
          <%= f.input :password_confirmation,
                      required: true,
                      input_html: { autocomplete: "new-password" } %>
          <%= f.input :first_name,
                      required: true %>
          <%= f.input :last_name,
                      required: true %>
          <%= f.input :bio,
                      required: true %>
          <%= f.input :photo, as: :file %>
          <%= cl_image_upload_tag(:image_id) %>
        </div>

              <div class="d-flex justify-content-end">
                <%= f.button :submit, "Sign up", class: 'big-button block' %>
              </div>
      <% end %>
      <div class="mt-3 mb-3">
        <%= render "devise/shared/links" %>
      </div>
    </div>
  </div>
</div>

如您所見,有 2 行不同的行來提示用戶上傳圖片。 他們似乎都從用戶的角度出發,但實際上從未將任何圖片上傳到我的雲端。

4/ 我的 development.rb 和 production.rb 文件中都有這一行:

config.active_storage.service = :cloudinary

5/ 我在 config/storage.yml 中添加了這一行

cloudinary:
  service: Cloudinary

6/我在我的終端中做了必要的步驟來為我的 heroku 配置 cloudinary:

heroku 配置:設置 CLOUDINARY_URL=cloudinary://166....

我錯過了什么嗎?

任何人的幫助將不勝感激! 奧利維爾

奧利維爾。 您是否將您的個人 API 密鑰放入應用程序的 .env 中?

CLOUDINARY_URL=cloudinary://API Key:API Secret

像這樣的東西:

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

檢查您的終端heroku config后,您必須看到CLOUDINARY_URL: cloudinary://....with_your_informations_...

您忘記將其添加為注冊中的強參數。 由於這是由設備處理的,因此您必須將其添加到應用程序 controller 中的方法中,如下所示:

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :bio, :photo])
end

暫無
暫無

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

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