(12/11 修正) 当初書いていた gakuseis_controller での記述だと edit の form_with の記述が Rails 風ではない気がしました. kurasus/gakuseis_controller での記述に修正します.
$ bin/rails g controller kurasus/gakuseis
Running via Spring preloader in process 27571
create app/controllers/kurasus/gakuseis_controller.rb
invoke haml
create app/views/kurasus/gakuseis
invoke rspec
invoke assets
invoke js
invoke css
$ bin/rails g integration_test gakuseis
Running via Spring preloader in process 52130
invoke rspec
create spec/requests/gakuseis_spec.rb
mkdir -p spec/requests/kurasus
mv spec/requests/gakuseis_spec.rb spec/requests/kurasus
Rails.application.routes.draw do
(中略)
resources :kurasus do
resources :gakuseis, controller: 'kurasus/gakuseis'
end
watch(rails.view_dirs) { |m| [rspec.spec.call("features/#{m[1]}"), rspec.spec.call("requests/#{m[1]}")] }
RSpec.describe :Gakuseis, type: :request do
include Devise::Test::IntegrationHelpers
let!(:object) { gakusei_factory :foo2301 }
let!(:others) { gakusei_factory :bar2302 }
let(:not_mine) { gakusei_factory :baz3301 }
let!(:kurasu) { object.kurasu }
let(:return_path) { kurasu_gakuseis_path(kurasu, edit_mode: true) }
context 'login by hkob' do
teacher_login :hkob
describe 'GET #index' do
context 'for my kurasu' do
context 'normal mode' do
subject { -> { get kurasu_gakuseis_path(kurasu) } }
it_behaves_like :response_status_check, 200
it_behaves_like :response_body_includes, %w[2300 2301 2302 編集モードに入る]
end
context 'edit mode' do
subject { -> { get kurasu_gakuseis_path(kurasu, edit_mode: true) } }
it_behaves_like :response_status_check, 200
it_behaves_like :response_body_includes, %w[2300 2301 2302 学生の一括登録]
end
end
context 'for not my kurasu' do
subject { -> { get kurasu_gakuseis_path(not_mine) } }
it_behaves_like :response_status_check, 302
it_behaves_like :response_body_not_includes, %w[3300 3301 学生の一括登録]
end
end
if false
(中略)
end
end
context 'not login' do
describe 'GET #index' do
subject { -> { get kurasu_gakuseis_path(kurasu) } }
it_behaves_like :response_status_check, 302
it_behaves_like :response_body_not_includes, %w[2300]
end
end
end
class GakuseisController < ApplicationController
before_action :authenticate_teacher!
before_action :get_kurasu
def index
redirect_to(kurasus_path, alert: t('.not_my_kurasu')) and return if @kurasu.nil?
@edit_mode = true if params[:edit_mode]
@kurasus = current_teacher.kurasus.order_name
@gakuseis = @kurasu.gakuseis.order_number
end
def get_kurasu
@kurasu = current_teacher.kurasus.find_by_id(params[:kurasu_id])
end
private :get_kurasu
end
- content_for :title do
- @title = "#{@kurasu.try(:name)}#{t '.title'}"
%table
%caption
- if @edit_mode
= link_to t('kurasus.gakuseis.new.title'), new_kurasu_gakusei_path(@kurasu)
- else
= link_to t('enter_edit_mode'), kurasu_gakuseis_path(@kurasu, edit_mode: true)
%thead
%tr
- t_ars(Gakusei, %i[number name]).each do |w|
%th= w
%th= t('control')
%tbody
- @gakuseis.each do |g|
%tr
%td= g.number
%td= g.name
%td
= link_to t('kurasus.gakuseis.show.title'), kurasu_gakusei_path(@kurasu, g)
- if @edit_mode
= link_to t('kurasus.gakuseis.edit.title'), edit_kurasu_gakusei_path(@kurasu, g)
= link_to t('.destroy'), kurasu_gakusei_path(@kurasu, g), method: :delete, data: {confirm: t('.confirm')}
= t '.select_kurasu'
%table
%tbody
- @kurasus.each do |k|
%tr
%td= link_to k.name, kurasu_gakuseis_path(k, edit_mode: @edit_mode.presence)
ja:
(中略)
control: 制御
enter_edit_mode: 編集モードに入る
(中略)
kurasus:
gakuseis:
index:
title: 学生一覧
select_kurasu: クラス選択
destroy: 学生削除
confirm: 学生を削除してよろしいですか?
not_my_kurasu: このクラスは担当していません.
show:
title: 学生表示
new:
title: 学生の一括登録
edit:
title: 学生編集
Gakuseis
login by hkob
GET #index
for my kurasu
normal mode
behaves like response_status_check
response should be 200
behaves like response_body_includes
response body includes ["2300", "2301", "2302", "編集モードに入る"]
edit mode
behaves like response_status_check
response should be 200
behaves like response_body_includes
response body includes ["2300", "2301", "2302", "学生の一括登録"]
for not my kurasu
behaves like response_status_check
response should be 302
behaves like response_body_not_includes
response body does not include ["3300", "3301", "学生の一括登録"]
not login
GET #index
behaves like response_status_check
response should be 302
behaves like response_body_not_includes
response body does not include ["2300"]
長くなったので今日はここまで