繁体   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