没有路线匹配[GET]“/ book / list”第二部分:Rails的路线复仇

时间:2014-05-28 19:07:52

标签: ruby-on-rails

我正在尝试关注此rails tutorial,但由于版本不匹配,我遇到了很多问题。我完全按照说明操作,直到现在我才真正了解所有内容,但我似乎无法自行解决此路由问题。 I read a good deal about routes independently但无法弄清楚该怎么做。之前已经在stackoverflow上发布了This question,但由于版本问题,该解决方案对我不起作用

错误消息:

No route matches [GET] "/book/list"

尝试访问http://localhost:3000/book/list时。

代码

route.rb

Rails.application.routes.draw do
  resources: :books
end

视图>书> list.rhtml

<% if @books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id="books">
<% @books.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Book", {:action => 'new' }%></p>

模型&GT; book.rb

class Book < ActiveRecord::Base
    belongs_to :subject
    validates_presence_of :title
    validates_numericality_of :price, :message=>"Error Message"
end

模型&GT; subject.rb中

class Subject < ActiveRecord::Base
    has_many :books
end

控制器&GT; book_controller.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

3 个答案:

答案 0 :(得分:8)

我看到了一些问题。例如,您的控制器应该是BooksController而不是BookController(您还需要确保它位于名为books_controller.rb的文件中)。第二,当你执行resources :books时,Rails将创建以下路由

GET /books -> index
GET /books/:id -> show
GET /books/:id/edit -> edit
PUT /books/:id -> update
GET /books/new -> new
POST /books -> create
DELETE /books/:id -> destroy

如您所见,list不是创建的路由之一,这就是您收到该错误消息的原因。

答案 1 :(得分:1)

您已在resources :books中定义routes。因此,您的控制器类名称应为多个,即。BooksController而非BookController。错误也是如此。

将您的控制器类名称更改为BooksController,将文件名更改为books_controller.rb

将您的routes更新为

resource :book #not singular

注意:我更喜欢第一种方式,因为它适合Rails convention

<强>更新

您必须将routes更新为喜欢此内容

resources :books do
  collection do
    get 'list'
  end
end

这将使Rails能够通过 /books/list 识别路径 GET ,并路由到 list < / strong> BooksController 的行动。

答案 2 :(得分:1)

延长@ BartJedrocha的答案,首先是你当前的路线,即

resources: :books

您的应用程序不起作用,并且会给您一个语法错误syntax error, unexpected ':', expecting keyword_end (SyntaxError)

由于resources是一个带参数:books的方法调用。 所以你的路线应该是

resources :books ## Notice no : after "resources"