Iam trying to insert some data in to database.but showing following error
Routing Error
No route matches [POST] "/book/create"
code for form submittion is new.html.erb
<h1>Add new book</h1>
<%= form_tag :action => 'create' %>
<p><label for="book_title">Title</label>:
<%= text_field 'book','title' %></p>
<p><label for="book_price">Price</label>:
<%= text_field 'book','price'%></p>
<p><label for="book_subject">Subject</label>:
<%= text_field 'subject','subject'%></p>
<p><label for="book_description">Description</label><br/>
<%= text_area 'book','description'%></p>
<%= submit_tag "Create"%>
<%= link_to 'Back',{:action=>'list'}%>
routers.rb is
Library::Application.routes.draw do
get "book/list"
get "book/show"
get "book/new"
get "book/create"
get "book/edit"
get "book/update"
get "book/delete"
resources :books, :only => [:new, :create]
match '/books' => 'books#create', :via => :post
Here is the html code for new.html.erb
<h1>Add new book</h1>
<form accept-charset="UTF-8" action="/book/create" method="post">
<p><label for="book_title">Title</label>:
<input id="book_title" name="book[title]" size="30" type="text" /></p>
<p><label for="book_price">Price</label>:
<input id="book_price" name="book[price]" size="30" type="text" /></p>
<p><label for="book_subject">Subject</label>:
<input id="subject_subject" name="subject[subject]" size="30" type="text" /></p>
<p><label for="book_description">Description</label><br/>
<textarea cols="40" id="book_description" name="book[description]" rows="20">
</textarea></p>
<input name="commit" type="submit" value="Create" />
<a href="/book/list">Back</a>
Here is the bookcontoller.rb
class BookController < ApplicationController
def list
@books = Book.find(:all)
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
@subjects = Subject.find(:all)
end
def create
@book = Book.new(params[:book])
if @book.save
redirect_to :action => 'list'
else
@subjects = Subject.find(:all)
render :action => 'new'
end
end
def edit
@book = Book.find(params[:id])
@subjects = Subject.find(:all)
end
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to :action => 'show', :id => @book
else
@subjects = Subject.find(:all)
render :action => 'edit'
end
end
def delete
Book.find(params[:id]).destroy
redirect_to :action => 'list'
end
def show_subjects
@subject = Subject.find(params[:id])
end
end
just include post "book/create"
in your router.rb file
From what I can see from your question you are fighting against Rails convention which makes everything harder. You should:
resources :books
in routes.rbform_for(@book)
which will generate the correct form to create a book in new.erb.I recommend a good read of the Rails guides.The following guides are especially relevant to your question and how to fix your problems:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With