5

荫试图插入一些数据database.but显示以下错误路由错误的路由匹配[POST]“/电子书/创建”

Routing Error 

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

的形式submittion代码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 
+0

你在控制中有什么r? def创造.....结束??它是吗? – swapnesh 2013-02-15 04:09:08

+0

包含控制器代码 – chinchu 2013-02-15 04:26:05

回答

12

刚在你的router.rb文件中包括post "book/create"

+0

是的,它增加了这条线 – chinchu 2013-02-28 06:34:18

0

清理你的routes.rb文件只是包含这个:

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

从我可以从你的问题,你正在打击看到Rails惯例使一切变得更加困难。你应该:

  • 只有resources :books在routes.rb中
  • 使用表单辅助form_for(@book)这将产生正确的形式创建一本书new.erb。

我推荐了Rails guides.The的下列指南很好看的是你的问题特别是相关的,如何解决你的问题:

相关问题