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

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

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

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

Q&A

2回答

2920閲覧

Ruby on Rails:CarrierWaveのaudioファイルアップロードができない。

frusciante

総合スコア8

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/10 10:34

前提・実現したいこと

現在、Ruby on Railsでアプリ開発をしており、CarrierWaveを使用して、mp3やwavなどの音声データをDBに保存したいと考えております。

以下の記事を参考に、記述をしてみたのですが、uninitialized constant CarrierWaveの NameError が表示されてしまいます。
参考記事リンク

お手数ですが、ご助言いただけますと幸いでございます。よろしくお願いいたします。

発生している問題・エラーメッセージ

エラー表示キャプチャ
※ちなみに、CarrierWaveでImageUploaderを使用した画像のアップロード記述も同じアプリ内で行なっておりますが、画像の方はDB保存まで成功しております。

該当のソースコード

  • musician.rb
class Musician < ApplicationRecord validates :name, :biography, :activity_history, :activity_place, :email ,presence: true validates :user_id, uniqueness: true has_many :matters has_many :chats has_many :messages belongs_to :user has_one :block mount_uploader :image, ImageUploader mount_uploader :sound_source, AudioFileUploader end
  • audio_file_uploader.rb
class AudioFileUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick # include CarrierWave::MiniMagick include CarrierWave::Audio # Choose what kind of storage to use for this uploader: storage :file # storage :fog version :mp3 do process :convert => [{output_format: :mp3}] def full_filename(for_file) "#{super.chomp(File.extname(super))}.mp3" end end version :wav do process :convert => [{output_format: :wav}] def full_filename(for_file) "#{super.chomp(File.extname(super))}.wav" end end # 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
  • musicians_controller.rb
class MusiciansController < ApplicationController def index end def new @musician = Musician.new end def create # @musician = Musician.create(musician_params) @musician = Musician.new(musician_params) @musician.save! end def edit end def update end private def musician_params params.require(:musician).permit(:image, :name, :biography, :activity_history, :activity_place, :email, :sound_source, :homepage, :twitter, :facebook, :other_link, :live_info, :office).merge(user_id: current_user.id) end end
  • Gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.4', '>= 5.2.4.1' # Use mysql as the database for Active Record gem 'mysql2', '>= 0.4.4', '< 0.6.0' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'rspec-rails', '~> 3.5' gem 'rails-controller-testing' gem 'factory_bot_rails' gem 'faker' gem 'capistrano' gem 'capistrano-rbenv' gem 'capistrano-bundler' gem 'capistrano-rails' gem 'capistrano3-unicorn' end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end group :production do gem 'unicorn', '5.4.1' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # create-haml-environment gem 'haml-rails' # FontAwesome able to use gem 'font-awesome-sass' # devise install gem 'devise' # Carrierwave Introduce gem 'carrierwave' gem 'mini_magick' gem 'pry-rails' gem 'fog-aws' gem 'jquery-rails'
画像アップ用のuploaderファイルのソース

※こちらは今回の「audioファイルアップロード」には関係していないかもしれませんが、CarrierWaveを使用した画像の保存は成功しているので、参考として記載させていただきます。

  • image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick # include CarrierWave::MiniMagick include CarrierWave::MiniMagick process resize_to_fit: [800, 800] # 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

試したこと

① config/environment.rbの末尾にrequire 'carrierwave/orm/activerecord'を追記。

以下記事を参考に再度試しましたが、エラーの表示は変わりませんでした。
参考記事リンク

② config/application.rbにconfig.autoload_paths += Dir[Rails.root.join('app', 'uploaders')]を追記。

以下記事を参考に追記後、サーバー再起動して試しましたが、エラーの表示は変わりませんでした。
参考記事リンク

③ Gemfileに'carrierwave'がある事を確認後、再度、bundle installを行う。

bundle install後、再度ブラウザを確認しましたが、エラーの表示は変わりませんでした。

④ audio_file_uploader.rb の記述を以下の様に修正して、再度ブラウザを確認。

こちら修正後、再度確認しましたが、同様のNameErrorが表示されてしまいました。
エラー画面キャプチャ

  • (修正前)audio_file_uploader.rb
include CarrierWave::Audio
  • (修正後)audio_file_uploader.rb
include CarrierWave::AudioFileUploader

開発環境

ruby 2.5.1
Rails 5.2.4.1

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

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

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

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

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

guest

回答2

0

CarrierWave::Audiocarrierwave-audio gemで用意されているモジュールです。
このgemをインストールしないと使えません。

投稿2020/02/12 01:50

Mugheart

総合スコア2344

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

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

0

以下は、いずれも記載なしで動作する気がするのですがいかがでしょう?

include CarrierWave::Audio include CarrierWave::AudioFileUploader

私は普段pdfなどのファイルもcarrierwaveでS3にアップロードしていますが、uploaderファイルにincludeは書いた記憶がありません。

imageの場合は、拡大縮小などの画像処理を行う場合のみ、上記のようなincludeが必要になるのでしょうが、単にファイルをそのままアップロードするだけであれば、そのような記述はなくても問題ないはずです。

(アップロード時に音声データに対する処理を行うつもりであれば、上記回答は無視していただいて構いません。)


それとは別件ですが、migrationファイルも掲載しておいた方がよいと思います。
sound_sourceカラムが追加されているかどうかが上記ソースからでは判断できないため...

投稿2020/02/12 01:36

siruku6

総合スコア1382

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問