具有 Rails CRUD 的最佳音频上传
The best Audio Upload with a Rails CRUD
我尝试在特定的 rails 新的简单格式中创建音频播放器。
只是一个简短的消息我想发布带有音频按钮的翻盖抽认卡(实际上我生成平假名)用音频播放器听单词。
我想问上传音频文件的最佳解决方案是什么?
实际上,我尝试使用 Cloudinary 视频存储服务来做到这一点。
我用 CarrierWave gem 创建了一个声音上传器,如下所示:
# encoding: utf-8
class SoundUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# 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
# # 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_white_list
# %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
然后在我的模型中,我用这段代码调用了 MountUploader(我调用的对象是声音):
class Hiragana < ActiveRecord::Base
belongs_to :user
has_many :favs, dependent: :destroy
mount_uploader :upload, ImageUploader
mount_uploader :sound, SoundUploader
end
上传是在查看新建中创建的
我的问题是如何调用上传?我的意思是我在我的代码中输入了这个变量 @hiragana.sound
但你能看一下错误图片 [!
我不确定我是否理解这里返回的错误是什么,但请注意,如果您使用 Cloudinary 将图像存储在云端,那么您需要省略(或注释)storage :file
和store_dir
函数。
如果这仍然没有帮助,请同时分享您的表单代码。
他的意思是你必须改变这个:
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
对此:
# Choose what kind of storage to use for this uploader:
# storage :file
# storage :fog
# def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end
我尝试在特定的 rails 新的简单格式中创建音频播放器。
只是一个简短的消息我想发布带有音频按钮的翻盖抽认卡(实际上我生成平假名)用音频播放器听单词。
我想问上传音频文件的最佳解决方案是什么? 实际上,我尝试使用 Cloudinary 视频存储服务来做到这一点。
我用 CarrierWave gem 创建了一个声音上传器,如下所示:
# encoding: utf-8
class SoundUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# 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
# # 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_white_list
# %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
然后在我的模型中,我用这段代码调用了 MountUploader(我调用的对象是声音):
class Hiragana < ActiveRecord::Base
belongs_to :user
has_many :favs, dependent: :destroy
mount_uploader :upload, ImageUploader
mount_uploader :sound, SoundUploader
end
上传是在查看新建中创建的
我的问题是如何调用上传?我的意思是我在我的代码中输入了这个变量 @hiragana.sound
但你能看一下错误图片 [!
我不确定我是否理解这里返回的错误是什么,但请注意,如果您使用 Cloudinary 将图像存储在云端,那么您需要省略(或注释)storage :file
和store_dir
函数。
如果这仍然没有帮助,请同时分享您的表单代码。
他的意思是你必须改变这个:
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
对此:
# Choose what kind of storage to use for this uploader:
# storage :file
# storage :fog
# def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end