[] unless File.exist?(FILE_PATH) File.read(FILE_PATH).split("\n").map.with_index(1) { |msg, idx| { id: idx, content: msg } } end # ファイルにメッセージを保存 def save_message(content) File.open(FILE_PATH, 'a') { |file| file.puts(content) } end set :messages, load_messages get '/' do @messages = settings.messages erb :index end post '/post' do content = params[:content].strip unless content.empty? save_message(content) settings.messages = load_messages end redirect '/' end