简体   繁体   English

Rails、Active Record、Devise 和 cloudinary、简单表格、heroku……照片没有上传到我的 cloudinary

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

Ok so, you get a gist of my problem in the title.好的,你在标题中得到了我的问题的要点。 I created an app in rails which requires users to create a profile and upload a picture in order to access some features.我在 Rails 中创建了一个应用程序,它要求用户创建个人资料并上传图片才能访问某些功能。 Anyway, everything works except the picture upload part.无论如何,除了图片上传部分,一切正常。

I am new to all this and I get a bit confused with Devise in particular.我对这一切都很陌生,特别是对 Devise 有点困惑。 I am looking for the user controller, which is where I should find the create action for the users right?我正在寻找用户 controller,我应该在哪里找到用户的创建操作,对吗? But I have no idea where to find it.但我不知道在哪里可以找到它。

That being said, I am not even sure the problem comes from the controller.话虽如此,我什至不确定问题是否来自 controller。 Could the source of the problem be simple form?问题的根源可能是简单的形式吗? No idea.不知道。 I have been searching and trying for quite a while now, and I thought it was time to ask the community:)我已经搜索和尝试了很长一段时间,我认为是时候询问社区了:)

I am going to share everything I can think of which concerns the problem.我将分享我能想到的与问题有关的所有内容。 Let me know if you need me to show anything else.如果您需要我展示其他内容,请告诉我。

1/ User model: 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/ Application controller: 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/ The new view where the simple form is located: 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>

As you can see there are 2 different lines to prompt the user to upload a picture.如您所见,有 2 行不同的行来提示用户上传图片。 They both seem to work on the user's point of view, but actually never upload any picture to my cloudinary.他们似乎都从用户的角度出发,但实际上从未将任何图片上传到我的云端。

4/ I have this line in both my development.rb and my production.rb file: 4/ 我的 development.rb 和 production.rb 文件中都有这一行:

config.active_storage.service = :cloudinary

5/ I added this line in config/storage.yml 5/ 我在 config/storage.yml 中添加了这一行

cloudinary:
  service: Cloudinary

6/ And I did the necessary steps in my terminal to configure cloudinary to my heroku: 6/我在我的终端中做了必要的步骤来为我的 heroku 配置 cloudinary:

heroku config:set CLOUDINARY_URL=cloudinary://166.... heroku 配置:设置 CLOUDINARY_URL=cloudinary://166....

Am I missing something?我错过了什么吗?

Anyone's help would be very much appreciated!任何人的帮助将不胜感激! Olivier奥利维尔

Olivier.奥利维尔。 Have you put your personal API key in the.env of your app?您是否将您的个人 API 密钥放入应用程序的 .env 中?

CLOUDINARY_URL=cloudinary://API Key:API Secret

something like this:像这样的东西:

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

After check in your terminal heroku config you must see CLOUDINARY_URL: cloudinary://....with_your_informations_...检查您的终端heroku config后,您必须看到CLOUDINARY_URL: cloudinary://....with_your_informations_...

You forgot to add it as a strong parameter in the registrations.您忘记将其添加为注册中的强参数。 Since this is handled by device, you have to add it to the method in your application controller like so:由于这是由设备处理的,因此您必须将其添加到应用程序 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