Roland
Roland

Reputation: 47

Routes for edit not working ruby

Hi am trying to edit/update some data in the database but I keep getting the following error:

No route matches [POST] "/books/11/edit" 

I tried adding some different lines the routes.rb such as

post 'books/edit'

etc.. but am having no luck. Here is my current Routes file.

Rails.application.routes.draw do

  resources :books, :only => [:new, :create, :edit, :index]

  resources :details, :only => [:new, :edit]

  resources :members

  devise_for :users 

  get 'page/books'

  get 'page/about'

  get 'page/contact'

  get 'page/home'

  post "details/new"

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
   root 'page#home'

My Def Edit is :

  def edit
    @book = Book.find(params[:id])
  end

And the form am using to used is:

<h4> Book Details </h4>

<p> Edit This Current Book </p>
<%= form_for @book do |f| -%>
 <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :isbn_number %><br />
    <%= f.text_field :isbn_number, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :author %><br />
    <%= f.text_field :author, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :status %><br />
    <%= f.text_field :status, autofocus: true %>
  </div>

  <br />
    <%= f.submit "Edit Book" %>
<% end -%>

  <br />

==Worked after adding this== Def Update:

def update
    @book = Book.find(params[:id])
    @book.update_attributes(book_params)
    redirect_to new_book_path
  end

Upvotes: 1

Views: 79

Answers (1)

Michał Szajbe
Michał Szajbe

Reputation: 9002

The form is rendered with edit action, but it is submitted to update action as PUT request. You need to implement it in your controller and handle it in routes.

resources :books, :only => [:new, :create, :edit, :update, :index]

Upvotes: 1

Related Questions