我正在使用Rails Active Storage,并且尝试将MP3文件附加到对象,但是出现以下错误:
ArgumentError (wrong number of arguments (given 0, expected 1)):
这是我当前拥有的代码:
def perform(streamplay)
text = File.open(streamplay.text_path).read
text = perform_regex(text)
convert = convert_to_audio(text)
audio = convert.audio_content
filename = streamplay.screenplay.title
# The response's audio_content is binary.
File.open "/output.mp3", "wb" do |file|
# Write the response to the output file.
file.write audio
end
streamplay.file.attach(
io: File.open("#{Rails.root}/output.mp3"),
filename: "#{filename}.mp3",
content_type: "audio/mpeg",
identify: false
)
end
我可以看到音频文件正在写入根目录,并且可以在计算机上收听它,因此该文件可以通过。正是在这段代码中,事情似乎正在崩溃:
streamplay.file.attach
提前感谢!
好,所以看起来问题实际上在这里的File.open
部分中:
File.open "/output.mp3", "wb" do |file|
# Write the response to the output file.
file.write audio
end
我必须在“ output.mp3”之前删除前面的“ /”。所以现在我有了它,它可以工作:
File.open "output.mp3", "wb" do |file|
# Write the response to the output file.
file.write audio
end