form_forで選んだ選択肢を保存したいのですができません。
コントローラー
ruby
1class SchedulesController < ApplicationController 2require "pry" 3 def new 4 @schedule = Schedule.new 5 end 6 def create 7 @schedule = Schedule.new(schedule_params) 8 if @schedule.save 9 binding.pry 10 flash.now[:success] = '登録が完了しました' 11 render :new 12 else 13 flash.now[:danger] = "登録に失敗しました" 14 render :new 15 end 16 end 17 private 18 def schedule_params 19 params.require(:schedule).permit(:selection,:start_time,:end_time,:break) 20 end 21end
マイグレーションファイル
class CreateSchedules < ActiveRecord::Migration[5.2] def change create_table :schedules do |t| t.string :selection t.time :start_time t.time :end_time t.time:break t.timestamps end end end
rails コンソール (手動)
[5] pry(main)> Schedule.create(selection: '休み') (0.3ms) BEGIN Schedule Create (25.7ms) INSERT INTO `schedules` (`selection`, `created_at`, `updated_at`) VALUES ('休み', '2018-12-05 07:45:24', '2018-12-05 07:45:24') (3.6ms) COMMIT => #<Schedule:0x00007fdd8e83e050 id: 1, selection: "休み", start_time: nil, end_time: nil, break: nil, created_at: Wed, 05 Dec 2018 07:45:24 UTC +00:00, updated_at: Wed, 05 Dec 2018 07:45:24 UTC +00:00>
ruby
1<%= form_for @schedule do |f| %> 2<%= f.select :selection, [["あ"], ["い"], ["う"]], :prompt => "選択してください" %> 3<%= f.time_field :start_time %> 4<%= f.time_field :end_time %> 5<%= f.time_field :break %> 6<%= f.submit "登録"%> 7%end%>
登録ボタンを押しても何も起きず、@scheduleはnilのままです。
こういった場合に、やるべき確認事項は何になるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。