简体   繁体   中英

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. Anyway, everything works except the picture upload part.

I am new to all this and I get a bit confused with Devise in particular. I am looking for the user controller, which is where I should find the create action for the users right? But I have no idea where to find it.

That being said, I am not even sure the problem comes from the 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:

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:

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:

<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. 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:

config.active_storage.service = :cloudinary

5/ I added this line in config/storage.yml

cloudinary:
  service: Cloudinary

6/ And I did the necessary steps in my terminal to configure cloudinary to my heroku:

heroku config:set 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?

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_...

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM