我还是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| %>
&lt;%= b.title%&gt;
显示的错误是:
无法找到没有ID的图书
我不明白控制器#show方法的含义 BooksController#show中的ActiveRecord :: RecordNotFound
答案 0 :(得分:0)
我找到了错误的来源,我忘了指定合适的路线