投稿を編集する画面で、submitをクリックするとルーティングエラーになります。
編集後は"/books/id(27)"に移動したいのですが、エラーでは"/book/id(27)"になっています。
"/books/id(27)"になるように修正したいです。
Routing Error No route matches [PATCH] "/book/27" Rails.root: /home/ec2-user/environment/bookers-level2.herokuapp Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom Helper HTTP Verb Path Controller#Action Path / Url book_path GET /books/:id(.:format) books#show PATCH /books/:id(.:format) books#update PUT /books/:id(.:format) books#update DELETE /books/:id(.:format) books#destroy
books_controller.rb class BooksController < ApplicationController before_action :authenticate_user!, only: [:show] def create @book = Book.new(book_params) @book.user_id = current_user.id @books = Book.all if @book.save flash[:notice] = "successfully" redirect_to user_url(current_user.id) else render :index end end def index @book = Book.new @books = Book.all end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def update book = Book.find(params[:id]) if book.update(book_params) flash[:notice] = "You have updated book successfully." redirect_to book_path(book.id) else render :edit end end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_url end private def book_params params.require(:book).permit(:title, :body) end end
books/edit.html.erb <%= render 'shared/header' %> <p>Editing Book</p> <%= form_for(book, url:"/books/#{book.id}"), method: :patch do |f| %> <p>Title</p> <%= f.text_field :title %> <p>Opinion</p> <%= f.text_area :body %> <%= f.submit 'Update Book' %> <% end %> <%= link_to "Show", book_path %> <p>|</p> <%= link_to "Back", books_path %> <%= render 'shared/footer' %>
routes.rb Rails.application.routes.draw do devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations' } get root 'top#top' get '/home/about', to: 'top#home' resources :users resources :books # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。