【发布时间】:2014-12-06 19:07:37
【问题描述】:
我是 ruby 和 rails 的新手,当我打开 localhost 链接 /book/list 时出现以下错误
ActiveRecord::RecordNotFound in BooksController#list
Couldn't find Book with 'id'=all
class BookController < ApplicationController
def list
@books = Book.find(:all) <------- problem is here
end
def show
@book = Book.find(params[:id])
我的 books_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
# private
def book_params
params.require(:book).permit(:title,:price,:subject,:description)
end
def create
@book = Book.new(book_params)
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 book1_params
params.require(:book).permit(:id)
end
def update
#below is the line I am getting the error....
@book = Book.find(params[:id])
if @book.update_attributes(book_params)
redirect_to @book
else
@subjects = Subject.all
render 'edit'
end
end
end
list 方法中的@books = Book.find(:all) 行告诉Rails 搜索books 表并将它找到的每一行存储在@books 实例对象中。我在列表中出现错误,new 和all几乎所有的选项。它说找不到'id'=all 的书。
我的 list.html.erb 文件。
<% 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>
我的 routes.rb 文件
Rails.application.routes.draw do
get 'book/new'
post 'book/create'
post 'book/update'
get 'book/list'
get 'book/show'
get 'book/edit'
get 'book/delete'
get 'book/update'
get 'book/show_subjects'
结束
我最近开始使用它,我在初始阶段非常感谢任何帮助。
【问题讨论】:
-
rails的哪个版本?你应该使用@books = Book.all -
我正在使用 4.1.8...您的解决方案刚刚解决了我的问题..我在过去 3 小时内一直在使用它..非常感谢..
-
您是否偶然使用了古老的 Rails2 教程?如果你是,你可能想从更现代的东西重新开始。
-
@soldiershin 无论你使用什么教程,放弃它。
find(:all)多年来一直不是查找所有记录的正确方法。 -
我特别关注了bcz教程点的那个,通常他们那里有一些不错的材料,但现在我知道了..
标签: ruby-on-rails ruby