【问题标题】:ActionController::UrlGenerationError in Book#newBook#new 中的 ActionController::UrlGenerationError
【发布时间】:2014-03-18 06:59:21
【问题描述】:
    I getting the following error while I am accessing my new method like http://127.0.0.1:3000/book/new 

我的项目名称是 LibraryWebProject 我正在使用带有 Rails 插件的 Eclipse

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数据库,gem是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'

..................... 没有路线匹配是什么意思我无能为力找到解决方案以及新的rails和ruby

【问题讨论】:

  • 你能用回溯添加整个错误吗?
  • 这里是整个错误 ActionView::Template::Error (No route matches {:action=>"create", :controller=>"book"}): 1:

    Add new book

    2: 'create' %> 3:

    : 4:

    5:

    : app/views/book/new.erb:2:in `_app_views_book_new_erb__586260966_11670096' 渲染 c:/RailsInstaller/Ruby1 .9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms) 渲染c:/RailsInstaller/Ruby1.9

  • 那么创建操作的路径在哪里?
  • 路由应该在 routes.rb 中是否正确 get 'book/new/create' 或 get 'book/create'
  • 两者都不是。您应该阅读 Rails 中的资源路由 - guides.rubyonrails.org/…

标签: ruby-on-rails-4


【解决方案1】:

你的路线错了

简单地替换为

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

【讨论】:

  • 我添加了 LibraryWebProject::Application.routes.draw do resources :books end 但它给了我这个错误 No route matches [GET] "/book/new" Rails.root: D:/RailsAppsExamples/ LibraryWebProject actionpack (4.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call' actionpack (4.0.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.0.3) lib/rails/rack/logger.rb:38:in `call_app'
  • 因为它不再是“书”,而应该是“书”。
  • 控制器应该是复数形式吗??因为你像书本而不是书本一样航行
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 2015-05-23
  • 2016-10-30
  • 2015-12-30
  • 2018-05-12
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
相关资源
最近更新 更多