Ruby on Rails 4:在搜索结果页面上显示搜索结果 [英] Ruby on Rails 4: Display Search Results on Search Results Page

查看:68
本文介绍了Ruby on Rails 4:在搜索结果页面上显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个项目中实现搜索功能,但我正在努力将其显示在专用搜索结果页面上.

已经意识到有关此主题的问题,但由于完全无能而无法找到解决方案,我请您提供最终指示:).

搜索表单在索引页上生成,即entries_path 和root_path.我想将参数传递给新页面 search_path.

这是我的文件:

EntriesController

定义搜索结尾定义索引@entries = Entry.all.order('entries.created_at DESC')@entry = Entry.new # 我的索引页也创建了新条目.如果参数[:搜索]@entries = Entry.search(params[:search]).order("created_at DESC")别的@entries = Entry.all.order("created_at DESC")结尾

型号:entry.rb

def self.search(search)where("content LIKE ? OR created_at LIKE ?", "%#{search}%", "%#{search}%")结尾

routes.rb

Rails.application.routes.draw 做资源:条目根'条目#索引'获取新"=>'条目/新'得到显示"=>'条目/显示'得到编辑"=>'条目/编辑'获取搜索"=>'条目/搜索'

最后:索引上的表单

<%= form_tag(entries_path, :method => "get", class: "search-form") do %><%= text_field_tag :search, params[:search], placeholder: "Search for previous entry..", class: "form-control" %><%结束%>

当我将entries_path 更改为search_path 时,我收到我们很抱歉,但出了点问题.如果您是应用程序所有者,请检查日志以获取更多信息."– 因此,我怀疑是路由问题.但是,我似乎无法弄清楚.日志说:

ActionController::RoutingError(未初始化的常量条目):

呼,很想知道这里发生了什么!已经非常感谢了.

解决方案

更改您的 routes.rb

Rails.application.routes.draw 做根'条目#索引'资源:条目做收藏做获取:搜索结尾结尾结尾

索引页面上的search表单中更改您的path:

<%= form_tag(search_entries_path, :method => :get, class: "search-form") do %><%= text_field_tag :search, params[:search], placeholder: "Search for previous entry..", class: "form-control" %><%结束%>

更改控制器的方法:

定义搜索如果参数[:搜索]@entries = Entry.search(params[:search]).order("created_at DESC")别的@entries = Entry.all.order("created_at DESC")结尾结尾

view/entries/search.html.erb下为搜索方法创建一个模板您可以在此处访问您的 @entries 对象

<小时>

我所做的更改点:

1.routes.rb 中的更改:

Rails 路由器识别 URL 并将它们分派给控制器的操作.它还可以生成 pathsURLs,避免在视图中硬编码字符串的需要.resource 路由将多个相关请求映射到单个控制器中的操作.资源丰富的路由提供了HTTP 动词URLs 到控制器动作的映射.根据约定,每个操作也映射到数据库中特定的CRUD 操作.关于路线的更多信息

您可以添加适用于collection集合的单个成员 的其他路由.

例如:

要添加成员路由,只需在资源块中添加一个成员块:

resources :entries do会员做获取预览"结尾结尾

要将路线添加到集合:

resources :entries do收藏做获取搜索"结尾结尾

一个成员路由需要一个ID,因为它作用于一个member.collection route 不会,因为它作用于 objectscollection.有关更多信息ruby on 中收集路由和成员路由的区别导轨?

2.我应该使用哪种方法搜索 GETPOST?

网络上有许多关于 GETPOST 请求的帖子以及 SO.GETPOST 都有自己的位置,如果您是 Web 开发人员,您应该了解它们各自的优缺点.或者,如果您懒得这样做,请记住 搜索表单应该使用 GET 方法.您的用户会欣赏它.;)

让我用简短的描述来定义它们.GET 获取资源(当您不想在数据库中进行任何更改时),POST 以创建资源(当您想要进行更改/创建时)在您的数据库中),PUT(或 PATCH,这是关于此事的争论)更新资源,DELETE 删除一个.>

供您参考:

希望这些信息对您有所帮助.祝你好运:)

I am currently implementing the search functionality in a project and I am struggling displaying it on a dedicated search result page.

Being aware of questions on this topic already but being unable to work out a solution due to utter incompetence, I am asking you for the final pointer :).

The search form spawns on the index page which is entries_path and root_path. I'd like to pass on the parameters to a new page, search_path.

Here are my files:

EntriesController

def search
end

def index
  @entries = Entry.all.order('entries.created_at DESC')
  @entry = Entry.new # My index page also creates new entries.

  if params[:search]
    @entries = Entry.search(params[:search]).order("created_at DESC")
  else
    @entries = Entry.all.order("created_at DESC")
  end

Model: entry.rb

def self.search(search)
  where("content LIKE ? OR created_at LIKE ?", "%#{search}%", "%#{search}%") 
end

routes.rb

Rails.application.routes.draw do
  resources :entries 
  root                  'entries#index'  
  get   'new'       =>  'entries/new'
  get   'show'      =>  'entries/show'
  get   'edit'      =>  'entries/edit'
  get   'search'    =>  'entries/search'

Finally: the form on index

<%= form_tag(entries_path, :method => "get", class: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search for previous entries..", class: "form-control" %>
<% end %>

When I change the entries_path to search_path, I am getting a "We're sorry, but something went wrong. If you are the application owner check the logs for more information." – therefore, I suspect it is a routing problem. However, I can't seem to figure it out. The log says:

ActionController::RoutingError (uninitialized constant Entries):

Phew, would love to know what's going on here! Thanks a bunch already.

解决方案

Change your routes.rb

Rails.application.routes.draw do
  root                  'entries#index'
  resources :entries  do
    collection do
      get :search
    end
  end
end

change your path in search form on index page:

<%= form_tag(search_entries_path, :method => :get, class: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search for previous entries..", class: "form-control" %>
<% end %>

Change your controller's method:

def search
  if params[:search]
    @entries = Entry.search(params[:search]).order("created_at DESC")
  else
    @entries = Entry.all.order("created_at DESC")
  end
end

create one template for search method under view/entries/search.html.erb You can here access your @entries object


Points of changes I have made:

1. Changes in routes.rb:

Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. A resource route maps a number of related requests to actions in a single controller. a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. for more information regarding routes

You can add additional routes that apply to the collection or individual members of the collection.

For Eg:

To add a member route, just add a member block into the resource block:

resources :entries do
  member do
    get 'preview'
  end
end

To add a route to the collection:

resources :entries do
  collection do
    get 'search'
  end
end

A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. for more info about difference between collection route and member route in ruby on rails?

2. Which method should I use for search GET or POST?

There are numbers of post available regarding GET and POST request on the web as well as SO. GET and POST both have their place, and if you’re a Web developer you should understand the pros and cons of each of them. Or if you’re too lazy to do that, just remember that Search forms should use GET method. Your users will appreciate it. ;)

Let me define them in short description. GET to fetch a resource(when you don't want to make any change in your DB), POST to create a resource(when you want to make a change/create in your DB), PUT (or PATCH, these is debate on the matter) to update a resource, DELETE to delete one.

For your reference:

I hope this information may helps you. Good Luck :)

这篇关于Ruby on Rails 4:在搜索结果页面上显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆