简体   繁体   English

如何在Rails中显示搜索表单的结果?

[英]How can I display the results of my search form in Rails?

I made a simple search form, that is basically searching through a couple of names on some items. 我做了一个简单的搜索表单,基本上是在某些项目上搜索几个名称。 I want to display the results on a new page, if the search query can be found in the name. 如果可以在名称中找到搜索查询,我想在新页面上显示结果。 I think it's really simple, just new to rails. 我认为这很简单,只是对Rails的新手。

post_controler.rb - post_controler.rb-

class PostsController < ApplicationController
def index
    @posts = Post.all
  if params[:search]
    @posts = Post.search(params[:search]).order("created_at DESC")
  else
    @posts = Post.all.order('created_at DESC')
  end
end
end

routes.rb - routes.rb-

Rails.application.routes.draw do
  get '/index' => 'posts#index'
  resources :post, :posts
end

application.html.erb - application.html.erb-

<!DOCTYPE html>
<html>
<head>
  <title>Trial</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= stylesheet_link_tag "defaults", media: "all", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

index.html.erb - index.html.erb-

    <html>
        <body>
                <div class="input-group">
                    <%= form_tag(posts_path, :method => "get", id: "search-form") do %>
                    <%= text_field_tag :search, params[:search], placeholder: "Search Posts", class: 'form-control' %>
                <span class="input-group-btn">
                     <%= submit_tag "Search", class: 'btn btn-success' %>
                </span>
                <% end %>
                </div>
                </form>

</body>
</html>

post.rb post.rb

class Post < ActiveRecord::Base
def self.search(search)
  where("name LIKE ?", "%#{search}%") 
end

end

You can put this code in your index.html.erb file. 您可以将此代码放在index.html.erb文件中。 It doesn't matter whether there is search parameter or not. 是否有搜索参数都没有关系。 Just put the following code below your form. 只需将以下代码放在表单下方即可。

<% @posts.each do |post| %>
  <p>
    <%= post.name %>
  </p> 
<% end %>

Your controller code is fine. 您的控制器代码很好。 But doing the following will make it efficient. 但是,执行以下操作将使其高效。 It will access database once. 它将访问数据库一次。

def index
  @posts = Post.all.order('created_at DESC')
  @posts = @posts.search(params[:search]) if params[:search].present?
end

Updated for redirecting to new page 已更新,可重定向到新页面

post_controller.rb post_controller.rb

def search
  @posts = Post.all.order('created_at DESC')
  @posts = @posts.search(params[:search]) if params[:search].present?
end

routes.rb routes.rb

resources :posts do
  member do
    post :search
  end
end

search.html.erb search.html.erb

<% @posts.each do |post| %>
  <p>
    <%= post.name %>
  </p> 
<% end %>

Hope it helps you. 希望对您有帮助。 And in the form_tag of index.html.erb change the path to search. 并在index.html.erb的form_tag中更改搜索路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM