Home>

I want to redirect to the details page after updating the data on the edit page, but I get the error "No route matches [PATCH]"/books/1/books/1 "". I want to patch/books/1, but it doesn't work.
I reviewed the routing, but I didn't think there was anything unusual about it.
I would like to know how this kind of display appears.
Thank you.

route
Rails.application.routes.draw do
  root to: "top # index"
  post "/ books" =>"books # create"
  get "/ books" =>"books # index"
  get "/ books /: id" =>"books # show", as:'show'
  get "/ books /: id/edit" =>"books # edit", as:'edit'
  patch "/ books /: id" =>"books # update"
end
controller
class BooksController</pre>
<pre><code>view
 Editing Book 
<% = form_with model: @ book, url: "books/# {@ book.id}", local: true do | f |%>
   title 
<% = f.text_field: title%>
   body 
<% = f.text_area: body%>
<p><% = f.submit'Update Book'%></p>
<% end%>
<br><% = link_to "Show", show_path (@book)%>|<% = link_to "Back", "/ books"%></br>
  • Answer # 1

    You have assembled the path yourself with the url: parameter you are passing to form_with.
    Since it is a relative path, it may be better to add a slash at the beginning.

    url: "books/# {@ book.id}"
    url: "/books/#{@book.id}"

    However, when developing with Ruby on Rails, it is recommended to use the URL helper instead of constructing the path directly. Reference: Rails guide

  • Answer # 2

    Well, it seems to be an ad hoc answer if you can fix the current source code.
    If you are willing to allow the time and the currently registered data to disappear, follow the steps below.Standard Rails implementationI recommend you to learn.


    rails g scaffold mybook title: string body: text

    rails db: migrate

    Access http: // localhost: 3000/mybooks and check various operations

    Compare mybooks controller with my books controller

    Check the difference between mybooks and books in routes.rb

    Compare the view files as well.

    If you can understand it while looking at this article etc.

    rails destroy scaffold mybookRun

    rails db: migrate: resetIt will be the state before starting 1. by executing.


    The important thing is not to be able to create apps using scaffold, but to make sure that you understand what the source of each line means and how it relates. think.