【发布时间】:2014-11-05 15:13:23
【问题描述】:
我还是 RoR 的新手,我正在尝试向我的应用程序添加一个简单的搜索表单。我就是这样进行的。
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
if params[:search]
@users = User.search(params[:search]).order("created_at DESC")
else
@users = User.order('created_at DESC')
end
end
def new
@book= Book.new
end
def create
@book= Book.new(books_params)
if @book.save
redirect_to admin_manage_path
else
render 'new'
end
end
def list
@books= Book.all
end
def show
end
private
def set_book
@book = Book.find(params[:id])
end
def books_params
params.require(:book).permit(:title, :author, :price, :resume)
end
end
我的书本模型
class Book < ActiveRecord::Base
def self.search(query)
where('title like ?', "%#{query}%")
end
end
显示搜索表单的我的 list.html.erb:
<% if current_user.present? %>
Welcome <%= current_user.pseudo %> | <%= link_to 'Déconnexion' , user_logout_path %>
<% end %>
<br>
<%= form_tag(books_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search books" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<br>
还有我的 index.html.erb 应该在哪里显示搜索结果:
<% @books.each do |b| %>
显示的错误是:
找不到没有 ID 的书
我不明白它指的是控制器#show 方法 ActiveRecord::RecordNotFound in BooksController#show
【问题讨论】: