Rails 4 issue: No route matches [PATCH] “/book.17”

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am working on an edit page which works. However, when I click the save button to save the changes I make, in rails 4, I get the following message: No route matches [PATCH] "/book.17" Any advice on how to fix this? I have been researching for a while and believe that it has something to do with my routes and not directing to the right page. Just not sure how I should change it. I have tried using patch/put instead of get for my edit action or putting patch 'books#update' to update but get the same error message. Any help will be appreciated! Here is the code:

Controller:

class BooksController < ApplicationController

def new
 #@book = Book.all
 @book = Book.new 
 @authors = Author.all
end

def edit 
  @book = Book.find_by_id(params[:id])
  @authors = Author.all
end 

def update 
 @book = Book.find_by_id(params[:id])
  if @book.update_attributes(book_params)
   flash[:success] = "Book Updated!"
   redirect_to @book
  else
      render 'edit'
  end 
end 

Routes Page:

Rails.application.routes.draw do

root 'welcome#index'

get 'author' => 'authors#new'

get 'name' => 'authors#show'

get 'book' => 'books#new'

get 'show' => 'books#show'

patch 'edit' => 'books#update'

resources :authors

resources :books

Edit Page:

<div class="move">
<h1>Update a book entryh2>

<div class="row">
<div class="col-md-6 col-md-offset-3">

<%= form_for(@book)  do |f| %>
  <%= render 'form' %>

  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control' %>
  div>

  <div class="form-group">
    <%= f.label :pub_date %>
    <%= f.text_field :pub_date, class: 'form-control' %>
  div>

  <div class="form-group">
    <%= f.label :publisher %>
    <%= f.text_field :publisher, class: 'form-control' 
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

Change the route

     patch 'edit' => 'books#update'

to

     patch '/book.:id/' => 'books#update' 

it solves the problem


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.