路由错误没有路由匹配[POST]“/ book / create”

时间:2013-02-15 04:04:47

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我试图将一些数据插入到数据库中。但显示以下错误

Routing Error

No route matches [POST] "/book/create"

表单提交的代码是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是

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

以下是new.html.erb的html代码

<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>

这是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

3 个答案:

答案 0 :(得分:13)

只需在router.rb文件中包含post "book/create"

答案 1 :(得分:2)

从我的问题中我可以看出,你正在反对Rails惯例,这使得一切都变得更难。你应该:

  • 仅在routes.rb
  • 中有resources :books
  • 使用表单助手form_for(@book)生成正确的表单,以便在new.erb中创建一本书。

我建议您仔细阅读Rails指南。以下指南特别与您的问题以及如何解决问题相关:

答案 2 :(得分:0)

清理routes.rb文件以包含此内容:

Library::Application.routes.draw do
  resources :books, :except => [:index] do
    collection do
      get :list
      get :show_subjects
    end
  end
end