7

高度な検索フォームを作成する最善の方法がわかりません。私はいくつかの方法を見て、インターネット上で良い検索をしましたが、ほとんどの提案が古くなっているため、それらを機能させることができません. すでに質問しましたが、具体的すぎて問題を解決できなかったと思います。1 つの検索ボタンでさまざまなテキスト ボックスとドロップダウン ボックスを検索したいと考えています。

EDIT2:

プロジェクトコントローラー:

def index
    @projects = Project.all

respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @projects }
    end
  end

def search
@project_search = Project.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 2, :page => params[:page])


end




  # GET /projects/1
  # GET /projects/1.json
  def show
    @project = Project.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @project }
    end
  end

  # GET /projects/new
  # GET /projects/new.json
  def new
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
  end

  # GET /projects/1/edit
  def edit
    @project = Project.find(params[:id])
  end

  # POST /projects
  # POST /projects.json
  def create

    @project = Project.new(params[:project])


    @project.client = params[:new_client] unless params[:new_client].blank?
    @project.exception_pm = params[:new_exception_pm] unless params[:new_exception_pm].blank?
    @project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?
    @project.role = params[:new_role] unless params[:new_role].blank?
    @project.industry = params[:new_industry] unless params[:new_industry].blank?
    @project.business_div = params[:new_business_div] unless params[:new_business_div].blank?

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render json: @project, status: :created, location: @project }
      else
        format.html { render action: "new" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /projects/1
  # PUT /projects/1.json
  def update
    @project = Project.find(params[:id])

    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project = Project.find(params[:id])
    @project.destroy

    respond_to do |format|
      format.html { redirect_to projects_url }
      format.json { head :no_content }
    end
  end




private

  helper_method :sort_column, :sort_direction
  def sort_column
    Project.column_names.include?(params[:sort]) ? params[:sort] : "project_name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end
end

検索ビュー:

<h1>Search</h1>

<%= form_tag search_path, method: :get do %>
<%= hidden_field_tag :direction, params[:direction] %>
  <%= hidden_field_tag :sort, params[:sort] %>
  <%= text_field_tag :project_name, params[:project_name] %>
  <%= text_field_tag :client, params[:client] %>
  <%= submit_tag "Search", name: nil %>
<% end %>



<table class = "pretty">
<table border="1">
  <tr>
    <th><%= sortable "project_name", "Project name" %> </th>
    <th><%= sortable "client", "Client" %></th>
    <th>Exception pm</th>
    <th>Project owner</th>
    <th>Tech</th>
    <th>Role</th>
    <th>Industry</th>
    <th>Financials</th>
    <th>Business div</th>
    <th>Status</th>
    <th>Start date</th>
    <th>End date</th>
<% if false %>
    <th>Entry date</th>
    <th>Edited date</th>
    <th>Summary</th>
    <th>Lessons learned</tStackh>
    <th>Customer benifits</th>
    <th>Keywords</th>
    <!th></th>
    <!th></th>
    <!th></th>
<% end %>
  </tr>

<% @project_search.each do |t| %>
  <tr>
    <td><%= t.project_name %></td>
    <td><%= t.client %></td>
    <td><%= t.exception_pm %></td>
    <td><%= t.project_owner %></td>
    <td><%= t.tech %></td>
    <td><%= t.role %></td>
    <td><%= t.industry %></td>
    <td><%= t.financials %></td>
    <td><%= t.business_div %></td>
    <td><%= t.status %></td>
    <td><%= t.start_date %></td>
    <td><%= t.end_date %></td>
<% if false %>
    <td><%= t.entry_date %></td>
    <td><%= t.edited_date %></td>
    <td><%= t.summary %></td>
    <td><%= t.lessons_learned %></td>
    <td><%= t.customer_benifits %></td>
    <td><%= t.keywords %></td>
<% end %>
    <!td><%#= link_to 'Show', project %></td>
    <!td><%#= link_to 'Edit', edit_project_path(project) %></td>
    <!td><%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />
<%= will_paginate (@project_search) %>


<%= button_to "Search Again?", search_path, :method => "get" %>

<%# end %>
<%= button_to "Home", projects_path, :method => "get" %>

Project.rb

class Project < ActiveRecord::Base
  attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech

validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech



def self.search search_term
 return scoped unless search_term.present?
 where find(:all, :conditions => ['project_name OR client LIKE ?', "%#{search_term}%"])
end

end

ルート:

FinalApp::Application.routes.draw do
resources :projects
match "search" => "projects#search", :as => :search
root :to => 'projects#index'
end

ご覧のとおり、アプリケーションが完成するまでにはまだかなり時間がかかります。次のフィールドで検索できる検索フォームを作成しようとしています: プロジェクト名、クライアント、ID、業界、役割、技術、プロジェクト所有者、ステータス、開始日、終了日、およびキーワード。検索フォームには、ユーザーが検索しているフィールドに応じて、テキスト ボックスまたはドロップダウン メニューが表示されます。各フィールドを連鎖させて、それらをすべて一度に検索したいと考えています。以前は、私のコードを理解しやすくするために、例として project_name と client のみを使用していました。うまくいけば、私がやろうとしていることを今見ることができます。

4

3 に答える 3

11

という新しいコントローラを作成できますsearch

検索フォーム:

<%= form_tag search_index_path, method: :get do %>
  <%= text_field_tag :project, params[:project] %>
  <%= text_field_tag :client, params[:client] %>
  <%= submit_tag "Search", name: nil %>
<% end %>

あなたのroutes.rbに含める:

get "search/index"

検索コントローラー:

def index
  #store all the projects that match the name searched
  @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
  #store all the clients that match the name searched    
  @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

@projectsこれで、と@clientsインデックス ビューで遊ぶことができます。

検索に一致しない場合、これらの変数が nil になる可能性があるため、注意してください。

Project編集-2つのモデルがあると仮定していますClient-新しいコントローラーを作成できない場合は、現在のコントローラーで検索アクションを作成できます。

def search
  #store all the projects that match the name searched
  @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
  #store all the clients that match the name searched    
  @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

また、検索ビューで@projectsとを使用できます。@clients

結果を別の場所 (indexビューなど) に表示しようとしている場合は、上記を正しいアクションに移動するだけです。

 def index
  ....
  #store all the projects that match the name searched
  @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
  #store all the clients that match the name searched    
  @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

EDIT 2 - OK、同じモデル内のフィールドの組み合わせで検索しようとしています:

検索方法を変更して、次の 2 つのフィールドを追加します。

def self.search(search_project, search_client) 
  return scoped unless search_project.present? || search_client.present?
  where(['project_name LIKE ? AND client LIKE ?', "%#{search_project}%", "%#{search_client}%"])
end

ただし、search_project または search_client が存在しない場合はスコープが返されることに注意して||ください。必要に応じて AND (&&) に変更できます。

また、 はAND両方が一致した場合にのみ返されます。つまり、検索の組み合わせです... 必要に応じて変更することもできORます。

検索フォームがある:

検索フォーム:

<%= form_tag search_index_path, method: :get do %>
  <%= text_field_tag :project, params[:project] %>
  <%= text_field_tag :client, params[:client] %>
  <%= submit_tag "Search", name: nil %>
<% end %>

次に、コントローラーはその組み合わせをモデルに送信する必要があります。

@project_search = Project.search(params[:project], params[:client]).all

問題が解決すると思います...

于 2012-07-23T10:10:08.900 に答える
1

アプリケーションでMetaSearchを使用してきましたが、非常に便利であることがわかりました。すでに検討したことがある場合、どのような問題がありましたか?

同じ作者のRansackもあり、MetaSearch の後継です。

于 2012-07-23T09:44:48.430 に答える
0

簡単な説明は、このレールキャストにあります

基本的に、params に特定のフィールドが含まれているかどうかをテストし、フィルターを作成する必要があります。以下の例を参照してください。

def find_products
  products = Product.order(:name)
  products = products.where("name like ?", "%#{keywords}%") if keywords.present?
  products = products.where(category_id: category_id) if category_id.present?
  products = products.where("price >= ?", min_price) if min_price.present?
  products = products.where("price <= ?", max_price) if max_price.present?
  products
end 

代替手段はRansackです。Ransack を使用すると、Ruby on Rails アプリケーション用の単純な検索フォームと高度な検索フォームの両方を作成できます

于 2018-02-19T09:08:05.640 に答える