我有两种模式,书籍和课程。在books_controller中,我有一种搜索方法,可以按其ISBN查找图书。
def searchbook
@searchedbook = Book.find_by_isbn(params[:q])
end
索引页面中有一个输入字段,显示所有书籍。所以我的index.html.erb有这个:
=form_tag({controller: "books", action: "searchbook"}, :method => :get) do
.search_field
%p
Search Using ISBN:
=text_field_tag :q
=button_tag 'Go' , class: 'search-button' , type: :submit
%br
我已将此添加到我的路线中:
match 'searchbook', to: 'books#searchbook', via: :get
我刚刚意识到我的searchbook.html.erb与show.html.erb完全相同。如何获得show.html.erb来呈现搜索书中的结果
由于您对Searchbook页面视图和表单提交具有相同的操作,因此您需要q
在URLsearchbook
操作中检查参数。因此,无论何时提交表单,它都会附加参数q
和@searchedbook
def searchbook
@searchedbook = Book.find_by_isbn(params[:q]) if params[:q].present?
end
在 searchbook.html.erb
-if @searchedbook
= render "show" # render the show partial
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句