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 entry</h2>
<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' %><br />
</div>
<div class="form-group">
<%= f.select(:author_id, @authors.collect {|a|
[ a.name, a.id ]}, {:include_blank => 'Please select an author'},
class: "form-control") %><br />
</div>
<%= f.submit 'Save Changes', class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
Finally, my rake routes:
Rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
author GET /author(.:format) authors#new
name GET /name(.:format) authors#show
book GET /book(.:format) books#new
show GET /show(.:format) books#show
edit GET /edit(.:format) books#edit
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
authors GET /authors(.:format) authors#index
POST /authors(.:format) authors#create
new_author GET /authors/new(.:format) authors#new
edit_author GET /authors/:id/edit(.:format) authors#edit
GET /authors/:id(.:format) authors#show
PATCH /authors/:id(.:format) authors#update
PUT /authors/:id(.:format) authors#update
DELETE /authors/:id(.:format) authors#destroy
Here is what my log is saying:
Started PATCH "/book.18" for ::1 at 2015-08-15 16:32:10 -0400
ActionController::RoutingError (No route matches [PATCH] "/book.18"):
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in
call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in
call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in
block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in
tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in
call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in call'
rack (1.6.4) lib/rack/runtime.rb:18:in
call'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
resources :authors
andresources :books
. – Beartech/books/17
, but it is getting/book.17
. Add your console output to the question. – BeartechBook.find_by_id(params[:id])
, you can just sayBook.find(params[:id])
.find
by itself impliesid
. The other forms offind_by_....
are deprecated in favor offind_by(column_name: 'something')
. – Beartech