轨道,活动记录,设计和cloudinary,简单的形式,heroku...照片不上传到我的cloudinary

hk8txs48  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(120)

好的,你可以从标题中了解到我的问题。我在Rails中创建了一个应用程序,它要求用户创建一个配置文件并上传一张图片,以便访问一些功能。无论如何,除了图片上传部分之外,一切都正常。
我是新手,尤其是对Devise有点困惑。我正在寻找用户控制器,这是我应该找到用户创建操作的地方,对吗?但是我不知道在哪里可以找到它。
话虽如此,我甚至不确定问题来自控制器。问题的根源会不会是简单的表单?不知道。我现在已经搜索和尝试了相当一段时间,我认为是时候问问社区了:)
我将分享我所能想到的与问题有关的一切。如果你需要我展示任何其他东西,请告诉我。
1/用户模型:

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/应用控制器:

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>

正如你所看到的,有两条不同的线提示用户上传图片,它们似乎都是从用户的Angular 出发的,但实际上从来没有上传任何图片到我的云数据库。
4/我在我的development.rb和production.rb文件中都有这一行:

config.active_storage.service = :cloudinary

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

cloudinary:
  service: Cloudinary

6/我在我的终端中做了必要的步骤来为我的heroku配置cloudinary:
如果您的配置文件中有错误,请重新启动。
我错过了什么吗?
任何人的帮助都将不胜感激!Olivier

eni9jsuy

eni9jsuy1#

你忘记在注册中添加它作为一个强参数。因为这是由设备处理的,所以你必须像这样把它添加到你的应用程序控制器的方法中:

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

piv4azn72#

您是否已将您的个人API密钥放入应用程序的.env文件中?
CLOUDINARY_URL=cloudinary://API Key:API Secret
就像这样:
CLOUDINARY_URL=cloudinary://298522693261255:Qa1ZfO4syfbOC-***********************8
在您的终端heroku config中检查后,您必须看到CLOUDINARY_URL: cloudinary://....with_your_informations_...

相关问题