質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2063閲覧

NoMethodError:undefined method `edit_book_path'について

yukitodesu3

総合スコア10

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/02/07 13:47

編集2021/02/07 14:29

アプリケーションのテストの際
NoMethodError:undefined method `edit_book_path'のエラーが出ました。
コードを見直してみたところこのように書いているコードは見当たらず、どこがエラーなのかもわからない状況です。

イメージ説明

books.contoroller.erb

class BooksController < ApplicationController def index @book = Book.new @books = Book.all end def show @book = Book.find(params[:id]) end def new # Viewへ渡すためのインスタンス変数に空のモデルオブジェクトを生成する。 end def edit @book = Book.find(params[:id]) @books = Book.all end def books end def create @books = Book.all @book = Book.new(list_params) if @book.save flash[:notice] = "Book was successfully destroyed." redirect_to book_path(@book.id) else @books = Book.all render 'index' end end def update @books = Book.all @book = Book.find(params[:id]) if @book.update(list_params) redirect_to book_path(@book.id) else render 'edit' end end def destroy book = Book.find(params[:id]) book.destroy redirect_to books_path end private def list_params params.require(:book).permit(:title, :body) end end

edit.html.rb

<div id="edit-title"> <div class="edit-logo"> <h1>Editing Book</h1> </div> <%= form_with model:@book,local:true,class: "main-item" do |f| %> <% if @book.errors.any? %> <h2 class="main-error"><%= @book.errors.count %>個のerrorsがあります<br></h2> <% @book.errors.full_messages.each do |msg| %> <li class="main-error2"><%= msg %><br/></li> <% end %> <% end %> <div class="edit-logo"> <%= form_with model:@book, url:"/books/#{@book.id}", local:true do |f| %> <div class="edit-title"> <p>Title</p> <%= f.text_field :title %> </div> <div class="edit-body"> <p>Body</p> <%= f.text_area :body %> </div> <%= f.submit '保存' %> <% end %> <% end %> </div> </div>

index.html.rb

<main> <!-- form_with部分 --> <% if flash[:notice] %> <div class="flash"> <%= flash[:notice] %> </div> <% end %> <div id="main-title"> <h1>Books</h1> </div> <table> <thead> <tr class="tr-title"> <th>Title</th> <th>body</th> <th></th> </tr> <thead> <% @books.each do |book| %> <tbody> <tr class="tr-tilte2"> <td class="td-tilte"><span><%= book.title %></span></td> <td class="td-title"><span><%= book.body %></span></td> <td><%= link_to 'Show', book_path(book) %></td> <td><%= link_to 'Edit', edit_books_path(book.id) %></td> <td><%= link_to "delete", book_path(book), method: :delete, data:{confirm:'Are you sure?'}%></td> </tr> </tbody> <% end %> </table> <div class="main-logo"> <h1>New book</h1> </div> <%= form_with model:@book,local:true,class: "main-item" do |f| %> <% if @book.errors.any? %> <h2 class="main-error"><%= @book.errors.count %>個のerrorsがあります<br></h2> <% @book.errors.full_messages.each do |msg| %> <li class="main-error2"><%= msg %><br/></li> <% end %> <% end %> <div class="main-list"> <h4>title</h4> <%= f.text_field :title %> </div> <div class="main-list2"> <h4>body</h4> <%= f.text_area :body %> <%= f.submit '投稿' %> <% end %> </div> </main>

show.html.rb

<div id="show-title"> <p>Book was successfully created.</p> <p><%= @book.title %></p> <p><%= @book.body %></p> <div class="show-list"> <%= link_to "edit", edit_books_path(@book.id) %> <%= link_to 'back','/books' %> </div> </div>

books_spec.rb

require 'rails_helper' describe '投稿のテスト' do let!(:book) { create(:book,title:'hoge',body:'body') } describe 'トップ画面(root_path)のテスト' do before do visit root_path end context '表示の確認' do it 'トップ画面(root_path)に一覧ページへのリンクが表示されているか' do expect(page).to have_link "", href: books_path end it 'root_pathが"/"であるか' do expect(current_path).to eq('/') end end end describe "一覧画面のテスト" do before do visit books_path end context '一覧の表示とリンクの確認' do it "bookの一覧表示(tableタグ)と投稿フォームが同一画面に表示されているか" do expect(page).to have_selector 'table' expect(page).to have_field 'book[title]' expect(page).to have_field 'book[body]' end it "bookのタイトルと感想を表示し、詳細・編集・削除のリンクが表示されているか" do (1..5).each do |i| Book.create(title:'hoge'+i.to_s,body:'body'+i.to_s) end visit books_path Book.all.each_with_index do |book,i| j = i * 3 expect(page).to have_content book.title expect(page).to have_content book.body # Showリンク show_link = find_all('a')[j] expect(show_link.native.inner_text).to match(/show/i) expect(show_link[:href]).to eq book_path(book) # Editリンク show_link = find_all('a')[j+1] expect(show_link.native.inner_text).to match(/edit/i) expect(show_link[:href]).to eq edit_book_path(book) # Destroyリンク show_link = find_all('a')[j+2] expect(show_link.native.inner_text).to match(/destroy/i) expect(show_link[:href]).to eq book_path(book) end end it 'Create Bookボタンが表示される' do expect(page).to have_button 'Create Book' end end context '投稿処理に関するテスト' do it '投稿に成功しサクセスメッセージが表示されるか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Create Book' expect(page).to have_content 'successfully' end it '投稿に失敗する' do click_button 'Create Book' expect(page).to have_content 'error' expect(current_path).to eq('/books') end it '投稿後のリダイレクト先は正しいか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Create Book' expect(page).to have_current_path book_path(Book.last) end end context 'book削除のテスト' do it 'bookの削除' do expect{ book.destroy }.to change{ Book.count }.by(-1) # ※本来はダイアログのテストまで行うがココではデータが削除されることだけをテスト end end end describe '詳細画面のテスト' do before do visit book_path(book) end context '表示の確認' do it '本のタイトルと感想が画面に表示されていること' do expect(page).to have_content book.title expect(page).to have_content book.body end it 'Editリンクが表示される' do edit_link = find_all('a')[0] expect(edit_link.native.inner_text).to match(/edit/i) end it 'Backリンクが表示される' do back_link = find_all('a')[1] expect(back_link.native.inner_text).to match(/back/i) end end context 'リンクの遷移先の確認' do it 'Editの遷移先は編集画面か' do edit_link = find_all('a')[0] edit_link.click expect(current_path).to eq('/books/' + book.id.to_s + '/edit') end it 'Backの遷移先は一覧画面か' do back_link = find_all('a')[1] back_link.click expect(page).to have_current_path books_path end end end describe '編集画面のテスト' do before do visit edit_book_path(book) end context '表示の確認' do it '編集前のタイトルと感想がフォームに表示(セット)されている' do expect(page).to have_field 'book[title]', with: book.title expect(page).to have_field 'book[body]', with: book.body end it 'Update Bookボタンが表示される' do expect(page).to have_button 'Update Book' end it 'Showリンクが表示される' do show_link = find_all('a')[0] expect(show_link.native.inner_text).to match(/show/i) end it 'Backリンクが表示される' do back_link = find_all('a')[1] expect(back_link.native.inner_text).to match(/back/i) end end context 'リンクの遷移先の確認' do it 'Showの遷移先は詳細画面か' do show_link = find_all('a')[0] show_link.click expect(current_path).to eq('/books/' + book.id.to_s) end it 'Backの遷移先は一覧画面か' do back_link = find_all('a')[1] back_link.click expect(page).to have_current_path books_path end end context '更新処理に関するテスト' do it '更新に成功しサクセスメッセージが表示されるか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Update Book' expect(page).to have_content 'successfully' end it '更新に失敗しエラーメッセージが表示されるか' do fill_in 'book[title]', with: "" fill_in 'book[body]', with: "" click_button 'Update Book' expect(page).to have_content 'error' end it '更新後のリダイレクト先は正しいか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Update Book' expect(page).to have_current_path book_path(book) end end end end

イメージ説明

試したこと
・コードの再確認

問題のコードが見当たらず、困っています。
回答よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suama

2021/02/07 14:21

rails routes とコマンドを打ってみて、表示結果を添付していただけますか?(ルーティングの設定です) また、メッセージは spec/system/books_spec.rb で発生しているので、このソースも貼っていただけますでしょうか。
suama

2021/02/07 14:32

ありがとうございます、拝見しました!
yukitodesu3

2021/02/07 14:33

よろしくお願いします。
guest

回答1

0

ベストアンサー

こんばんは。
テストコードの添付、ルーティングの設定の追記ありがとうございます。

rails s でアプリケーションを起動した場合、画面は問題なく見えているでしょうか。(一覧表示、個別表示、編集画面)

rails routesの内容を見ると、「編集」用のPrefixが edit_book ではなく edit_books になっているので、呼び出し方も edit_books_path になりますね。
index.html.erbやshow.html.erbでは、<%= link_to "edit", edit_books_path(@book.id) %> という具合に記載されていて、エラーも無いはずです。

一方で、テストコード側は、編集の動作テストの箇所が、以下のようになっています。

ruby

1 describe '編集画面のテスト' do 2 before do 3 visit edit_book_path(book) 4 end 5 .... 6 end

エラーになっているのは、この visit edit_book_path(book) の部分です。

ここを一旦 visit edit_books_path(book) にしてみてはどうでしょう。

投稿2021/02/07 14:39

suama

総合スコア1997

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukitodesu3

2021/02/07 14:45

治りました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問