我一直在自学Ruby,而且我试图将信息输出到文本文件时相当困难。该文件被创建,没有错误抛出,但没有任何被写入文件。我选择修改文件与写入文件,因为我不希望数据被覆盖。我知道这将是一个简单的答案,但我现在一直在撞墙。提前致谢。将数据输出到本地文件时出现问题
这里是我的控制器
class EntriesController < InheritedResources::Base
actions :new, :show, :index, :edit, :update
before_filter :set_message, :only => [:update]
def save_to_file
playlist = 'playlist.txt'
File.open(playlist, 'a+') do |entry|
entry.each do |entry|
f.puts '#{entry.song} by #{entry.artist} on #{entry.album}'
end
end
end
def create
@entry = Entry.new(entry_params)
if @entry.save
flash[:notice] = "Your song was added"
save_to_file
redirect_to entries_path
else
flash[:error] = "Your song wasn't added. Please check all info and try agian."
render :new
end
end
private
def entry_params
params.require(:entry).permit(:artist, :album, :song)
end
def set_message
flash[:warning] = "Are you sure you want to overwrite this song information?"
end
end
我无法感谢您的耐心和周到的答复。我很确定我的错误在方法参数或基本逻辑的某处存在,但是有足够的错误导致我无法调试它。非常感谢你。 – jirgens
非常欢迎:) –