CRUD を作成⑨
CRUD を作成⑧ の続き。
今回は編集ページを作成する。
app/controllers/books_controller.rb を以下のように修正する。
def edit
@book = Book.find(params[:id])
end
テストを実行する。レッドになる。
bundle e rspec spec/requests/books_spec.rb
....F..
Failures:
1) Books GET /books/:id/edit returns http success
Failure/Error: @book = Book.find(params[:id])
ActiveRecord::RecordNotFound:
Couldn't find Book with 'id'=1
# ./app/controllers/books_controller.rb:24:in `edit'
# /usr/local/bundle/gems/actiontext-7.0.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
# /usr/local/bundle/gems/actiontext-7.0.4.1/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
# /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:36:in `block in call'
# /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `catch'
# /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/tempfile_reaper.rb:15:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/etag.rb:27:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/conditional_get.rb:27:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/head.rb:12:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/session/abstract/id.rb:266:in `context'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/session/abstract/id.rb:260:in `call'
# /usr/local/bundle/gems/railties-7.0.4.1/lib/rails/rack/logger.rb:40:in `call_app'
# /usr/local/bundle/gems/railties-7.0.4.1/lib/rails/rack/logger.rb:25:in `block in call'
# /usr/local/bundle/gems/railties-7.0.4.1/lib/rails/rack/logger.rb:25:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/method_override.rb:24:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/runtime.rb:22:in `call'
# /usr/local/bundle/gems/rack-2.2.6.2/lib/rack/sendfile.rb:110:in `call'
# /usr/local/bundle/gems/railties-7.0.4.1/lib/rails/engine.rb:530:in `call'
# /usr/local/bundle/gems/rack-test-2.0.2/lib/rack/test.rb:358:in `process_request'
# /usr/local/bundle/gems/rack-test-2.0.2/lib/rack/test.rb:155:in `request'
# ./spec/requests/books_spec.rb:45:in `block (3 levels) in <main>'
Finished in 0.3567 seconds (files took 2.09 seconds to load)
7 examples, 1 failure
Failed examples:
rspec ./spec/requests/books_spec.rb:44 # Books GET /books/:id/edit returns http success
現状はテストコードに存在しない id を指定しているため、以下のように spec/requests/books_spec.rb を修正する。
describe "GET /books/:id/edit" do
let(:book) { create(:book) }
it "returns http success" do
get "/books/#{book.id}/edit"
expect(response).to have_http_status(:success)
end
end
テストを実行するとグリーンになる。
bundle e rspec spec/requests/books_spec.rb
.......
Finished in 0.36925 seconds (files took 2.84 seconds to load)
7 examples, 0 failures
次に app/views/books/edit.html.erb を修正し、編集用のフォームを作成する。ついでに一覧へのリンクもこのタイミングで追加しておく。
<%= form_with model: @book, local: true do |form| %>
<%= form.text_field :title %>
<%= form.submit '更新' %>
<% end %>
<br />
<%= link_to '一覧', books_path %>
テストを実行する。グリーンになる。
bundle e rspec spec/requests/books_spec.rb
.......
Finished in 0.37214 seconds (files took 2.86 seconds to load)
7 examples, 0 failures
次に、更新処理を追加していく。
app/controllers/books_controller.rb を以下のように修正する。
def update
@book = Book.find(params[:id])
if @book.update(book_params)
redirect_to edit_book_path(@book)
else
render :edit
end
end
テストを実行する。レッドになる。
spec/requests/books_spec.rb を以下のように修正する。
describe "PATCH /books/:id" do
let(:book) { create(:book) }
let(:params) { { book: { title: } } }
let(:title) { 'test' }
it "returns http no_content" do
patch "/books/#{book.id}", params: params
expect(response).to have_http_status(:found)
end
end
テストを実行する。グリーンになる。
現状、あるページからあるページへ遷移する際、直接URLを指定することが多いため、それぞれのページにそれぞれのページへのリンクを作成する。
app/views/books/index.html.erb を以下のように修正。
<%= link_to '新規作成', new_book_path %><br />
<% @books.each do |book| %>
<%= link_to book.title, book_path(book) %>
<%= link_to '編集', edit_book_path(book) %><br />
<% end %>
app/views/books/show.html.erb を以下のように修正。
<%= link_to '新規作成', new_book_path %><br />
<%= link_to '一覧', books_path %><br />
<%= link_to '編集', edit_book_path(@book) %><br />
app/views/books/edit.html.erb を以下のように修正。
<%= link_to '新規作成', new_book_path %><br />
<%= link_to '一覧', books_path %><br />
<%= link_to '詳細', book_path(@book) %><br />
これでかなりページ間の行き来がしやすくなった。
次に続く。
この記事が気に入ったらサポートをしてみませんか?