resourcesでルーティングを設定した所エラーになりました。
delete後に/booksの一覧ページ(index.html.erb)へ飛ぶようにしたいのですがうまくいきません。
以下エラー文とコードになります。
**Routing Error No route matches [DELETE] "/books.1" Rails.root: /home/ec2-user/environment/Bookers** Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom Helper HTTP Verb Path Controller#Action root_path GET / top#top blogs_path GET /blogs(.:format) blogs#index POST /blogs(.:format) blogs#create new_blog_path GET /blogs/new(.:format) blogs#new edit_blog_path GET /blogs/:id/edit(.:format) blogs#edit blog_path GET /blogs/:id(.:format) blogs#show PATCH /blogs/:id(.:format) blogs#update PUT /blogs/:id(.:format) blogs#update DELETE /blogs/:id(.:format) blogs#destroy>
controller
class BlogsController < ApplicationController def index @books = Book.all @book = Book.new end def create book = Book.new(book_params) book.save redirect_to books_path(book.id) end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def destroy book = Book.find(params[:id]) book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body) end end
routes
Rails.application.routes.draw do get root 'top#top' resources :blogs # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
index.html.erb
<h1>Books</h1> <table> <thead> <tr> <th>Title</th> <th>Body</th> <th colspan="2"></th> </tr> </thead> <tbody> <% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.body %></td> <td><%= link_to 'Show', blog_path %></td> <td><%= link_to 'Edit', edit_book_path(book.id) %></td> <td><%= link_to 'Destroy', blogs_path(book.id), method: :delete, "data-confirm" => "本当に削除しますか?" %></td> </tr> <% end %> </tbody> </table> <h1>New book</h1> <%= form_for(@book) do |f| %> <h4>Title</h4> <%= f.text_field :title %> <h4>Body</h4> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %>
回答3件
あなたの回答
tips
プレビュー