ActionController::UrlGenerationError in Book#new

2019-08-22 03:12发布

问题:

    I getting the following error while I am accessing my new method like http://127.0.0.1:3000/book/new 

My project name is LibraryWebProject I am Using Eclipse with rails plugins

routes.rb file is

    LibraryWebProject::Application.routes.draw do
      get 'book/new'
      get 'book/list'
      get 'book/edit'
      get 'book/show_subjects'
      get 'book/show'
    end

my controller is

book_controller.rb controller contains the all methods. I used Mysql database and gem is mysql2

    class BookController < ApplicationController
       def list
          @books = Book.find(:all)
       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

       end


    end

my new.erb file is

<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>
<%= submit_tag "Create" %>
<%= end_form_tag %>
<%= link_to 'Back', {:action => 'list'} %>

I am Using Rails4.0.3

finally the error is

No route matches {:action=>"create", :controller=>"book"}
Extracted source (around line #2):
  <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>:

Rails.root: D:/RailsAppsExamples/LibraryWebProject

Application Trace | Framework Trace | Full Trace
actionpack (4.0.3) lib/action_dispatch/journey/formatter.rb:39:in `generate'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:601:in `generate'

..................... what is mean by No route matches I am helpless to find the solution for this and new to rails as well as ruby

回答1:

Your route is wrong

simply replace with

LibraryWebProject::Application.routes.draw do
  resources :books
end