Reputation: 1620
I am new to ruby as well as rails I am getting the following error when i open the localhost link /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])
My books_controller.rb file.
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
The @books = Book.find(:all) line in the list method tells Rails to search the books table and store each row it finds in the @books instance object.I am getting error in the list,new and all the nearly all the options.It says that couldnt find book with 'id'=all.
my list.html.erb file.
<% 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>
My routes.rb file
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'
end
I have recently started working with it and i m at the initial stage any help is appreciated.
Upvotes: 0
Views: 737
Reputation: 905
The .find()
method takes a primary key as its argument. When you pass Book.find(:all)
, your app is attempting to find an instance of Book
with an id of "all," hence the error message you are seeing.
As Anthony points out, you should be using Book.all
to return all instances of Book.
You can use this answer as a reference when trying to search through your model instances in the future.
Upvotes: 2