6

データベースにデータを挿入しようとしていますが、次のエラーが表示されます

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
4

3 に答える 3

14

post "book/create"router.rbファイルに含めるだけ です

于 2013-02-28T06:31:31.227 に答える
2

あなたの質問から私が見ることができることから、あなたはすべてを困難にするRailsの慣習と戦っています。あなたがすべき:

  • resources :booksroutes.rb にのみあります
  • form_for(@book)正しいフォームを生成するフォーム ヘルパーを使用して、new.erb にブックを作成します。

Railsガイドをよく読むことをお勧めします.次のガイドは、あなたの質問と問題の解決方法に特に関連しています:

于 2013-02-15T12:41:37.637 に答える
0

これだけを含めるように routes.rb ファイルをクリーンアップします。

Library::Application.routes.draw do
  resources :books, :except => [:index] do
    collection do
      get :list
      get :show_subjects
    end
  end
end
于 2013-02-15T05:52:51.817 に答える