ActionController的:: UrlGenerationError在书#新(ActionC

2019-11-03 02:22发布

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

我的项目名称是LibraryWebProject我使用Eclipse与Rails插件

routes.rb文件

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

我的控制器

book_controller.rb控制器包含的所有方法。 我使用的MySQL数据库和宝石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

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

我使用Rails4.0.3

终于误差

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'

.....................通过无路由匹配我无奈找到这个新的解决方案,以轨道以及红宝石什么是平均

Answer 1:

您的路线是错误的

简单地替换

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


文章来源: ActionController::UrlGenerationError in Book#new