質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

3210閲覧

[エラー]uninitialized constant User (call 'User.connection' to establish a connection)::Image

YutoKubo

総合スコア13

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/28 07:32

Carriawaveを導入しようとする中、確認のために新規登録画面から登録しリダイレクトするときに以下のようなエラーが発生しました。

uninitialized constant User (call 'User.connection' to establish a connection)::ImageUploader

こちらのエラーの特徴として、routesにdevise_for :users という記述がありますが、これを消すとエラーが一旦消えます。

 しかし、入れ違いで以下のようなエラーが発生します。こちらは、devise_for :usersがないと発生します。

undefined method `user_signed_in?' for #<#<Class:0x00007f9892ace2c0>:0x00007f9892acc358>

試した方法: config/environment.rbの末尾に次のコードを挿入しましたが変化はありませんでした。

require 'carrierwave/orm/activerecord'

<routes.rb>

Rails.application.routes.draw do resources 'users' devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: 'home#index' resources :mypage, only: [:profile] do member do get 'profile' end end end

<user.rb>

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable mount_uploader :profile_image, ImageUploader end

<uploaders/profile_image_image.uploader.rb>

class ProfileImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick process resize_to_fit: [100, 100] # Choose what kind of storage to use for this uploader: storage :file # storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url(*args) # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process resize_to_fit: [50, 50] # end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: # def extension_whitelist # %w(jpg jpeg gif png) # end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

エラーメッセージの通り、ImageUploaderが存在しません。

ProfileImageUploaderを作ったのでしたら、mount_uploaderでそれを指定すべきなのではないでしょうか?

投稿2020/12/28 07:33

maisumakun

総合スコア145121

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YutoKubo

2020/12/28 07:55 編集

'mount_uploaderでそれを指定すべき・・' <user.rb>の mount_uploader :profile_image, ImageUploader のことではないのでしょうか? ちゃんとあると思うのですが。。
guest

0

自己解決

クラス名ProfileImageUploaderをmountして
mount_uploader :profile_image, ProfileImageUploader 
とすることで解決しました。

投稿2020/12/28 08:22

YutoKubo

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問