💡
      
  
    この記事は Middleman 時代に書いた古いものです。記録のため、astro-notion-blog に移行していますが、あまり参考にしないでください。
    
  
event_spec の記述(new と create)
spec/requests/kurasus/event_spec.rb を修正する.ほぼこれも以前と同じである.
describe 'GET #new' do
  context 'my kurasu' do
    subject { -> { get new_kurasu_event_path(kurasu) } }
    it_behaves_like :response_status_check, 200
  end
  context 'not my kurasu' do
    subject { -> { get new_kurasu_event_path(not_mine.kurasu) } }
    it_behaves_like :response_status_check, 302
  end
end
describe 'POST #create' do
  before { @attrs = object.attributes; object.destroy }
  subject { -> { post kurasu_events_path(kurasu), params: {event: @attrs}} }
  context '正しいパラメータに対して' do
    it_behaves_like :response_status_check, 302
    it_behaves_like :increment_object_by_create, Event
    it_behaves_like :redirect_to
    it_behaves_like :flash_notice_message, 'イベントを登録しました.'
  end
  context '不正なパラメータに対して' do
    before { @attrs['name'] = nil }
    it_behaves_like :response_status_check, 200
    it_behaves_like :not_increment_object_by_create, Event
    it_behaves_like :flash_alert_message, 'イベントが登録できません.'
  end
end
        
  event_controller の記述(new と create)
app/controllers/kurasus/events_controller.rb を記述する.
def new
  @kurasus = current_teacher.kurasus.order_name
  redirect_to kurasus_path and return if @kurasu.nil?
  max_so = @kurasu.events.date_value_is(Date.today).maximum(:sort_order)
  @event = @kurasu.events.build(name: 'イベント名', date: Date.today, sort_order: max_so ? max_so + 1 : 1)
end
def create
  @event = Event.new(event_params)
  if @event.save
    redirect_to kurasu_events_path(@kurasu, edit_mode: true), notice: notice_message(Event)
  else
    flash.now[:alert] = alert_message(Event)
    render action: :new
  end
end
(中略)
def event_params
  params.require(:event).permit(:kurasu_id, :name, :date, :sort_order)
end
private :event_params
        
  new の view を作成
- 
        app/views/kurasus/events/new.html.haml を記述する.
        
- content_for :title do - @title = "#{t '.title'}: #{@kurasu.name}" = render partial: 'form', locals: {event: @event, kurasu: @kurasu} - 
        app/views/kurasus/events/_form.html.haml を記述する(後日 12/21 に修正する).
        
= form_with model: [@kurasu, event], local: true do |f| %table %tr %th= f.label :name %td= f.text_field :name, autofocus: true %tr %th= f.label :date %td= f.text_field :date %tr %th= f.label :sort_order %td= f.text_field :sort_order %tr %td{colspan: 2} = f.submit 
guard の結果
guard の結果は以下のようになった
GET #new
  my kurasu
    behaves like response_status_check
      response should be 200
  not my kurasu
    behaves like response_status_check
      response should be 302
POST #create
  正しいパラメータに対して
    behaves like response_status_check
      response should be 302
    behaves like increment_object_by_create
      should change `Event.count` by 1
    behaves like redirect_to
      should redirect to "/kurasus/1902/events?edit_mode=true"
    behaves like flash_notice_message
      should eq "イベントを登録しました."
  不正なパラメータに対して
    behaves like response_status_check
      response should be 200
    behaves like not_increment_object_by_create
      should not change `Event.count`
    behaves like flash_alert_message
      should eq "イベントが登録できません."
        
  長くなったので今日はここまで