2012-07-23 15 views
5

मुझे एक उन्नत खोज फ़ॉर्म बनाने का सबसे अच्छा तरीका समझने में परेशानी हो रही है। मेरे पास कुछ तरीकों से देखकर इंटरनेट पर अच्छी खोज है, लेकिन मैं उन्हें काम पर नहीं ला सकता, क्योंकि अधिकांश सुझाव पुराने हैं। मैंने पहले से ही एक सवाल पूछा है, लेकिन मुझे लगता है कि मैं बहुत विशिष्ट था और मैं अपनी समस्या को ठीक करने में सक्षम नहीं था। मैं अलग-अलग टेक्स्ट बॉक्स पर खोज करना चाहता हूं और एक खोज बटन के साथ बक्से ड्रॉप करना चाहता हूं।रेल पर रूबी: उन्नत खोज

EDIT2:

projects_controller:

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 

जैसा कि आप देख सकते हैं, मैं अभी भी एक पूर्ण आवेदन करने से काफी दूर हूं। मैं एक खोज फ़ॉर्म बनाने की कोशिश कर रहा हूं जो निम्नलिखित क्षेत्रों पर खोज करने में सक्षम होगा: परियोजना का नाम, ग्राहक, आईडी, उद्योग, भूमिका, प्रौद्योगिकी, परियोजना के मालिक, स्थिति, प्रारंभ तिथि, समाप्ति दिनांक और कीवर्ड। खोज फ़ॉर्म में टेक्स्ट बॉक्स या ड्रॉप डाउन मेनू होगा, इस पर निर्भर करता है कि उपयोगकर्ता किस क्षेत्र में खोज कर रहा था। मैं प्रत्येक क्षेत्र को चेन करना चाहता हूं और उन सभी को एक ही बार में खोजना चाहता हूं। इससे पहले, मैं केवल प्रोजेक्ट_नाम का उपयोग कर रहा था, और क्लाइंट उदाहरण के रूप में आपके कोड को समझना आसान बनाता था। उम्मीद है कि अब आप देख सकते हैं कि मैं क्या करने की कोशिश कर रहा हूं।

+0

क्या आपने इस पर [रेलसकास्ट] (http://railscasts.com/episodes/111- उन्नत- खोज-form-revised)? यह आपको मदद कर सकता है। –

उत्तर

8

आप 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 में incude:

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 साथ खेल सकते हैं इंडेक्स व्यू

बस सावधान रहें, क्योंकि खोज के लिए कोई मिलान नहीं होने पर ये चर शून्य हो सकते हैं।

संपादित करें - यदि आप एक नया नियंत्रक आप अपने वर्तमान नियंत्रक में खोज कार्रवाई बना सकते हैं नहीं बना सकते हैं - मैं यह सोचते हैं रहा हूँ आप दो मॉडल Project और 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 

संपादित 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_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 

मुझे लगता है कि समस्या का समाधान होगा

खोज प्रपत्र के बाद। ..

+0

हाय फिर से गेब्रियल, मैंने अपना खोज फ़ॉर्म उस सुझाव में बदल दिया जो आपने मुझे दिया था, हालांकि मैंने पथ को वही रखा, क्योंकि मैं एक नया खोज नियंत्रक बनाने के लिए संघर्ष कर रहा हूं। जब मैंने केवल एक टेक्स्ट बॉक्स था तो काम करने से पहले जिस तरह से मेरा आवेदन सेट किया गया था। निश्चित रूप से यह एक और जोड़ना मुश्किल नहीं हो सकता है, और एक साथ खोज को चेन कर सकता है? – Jazz

+1

मैं आपके पिछले [प्रश्न] (http://stackoverflow.com/questions/11577937/ruby-on-rails-search-form-multiple-search-fields) देख रहा था और मुझे समझ में नहीं आया कि आपके पास एक या दो है मॉडल के। सुझाया गया उत्तर परियोजना मॉडल के अंदर ग्राहक नाम खोजने की कोशिश कर रहा है और यदि आपके पास दो मॉडल हैं तो यह काम नहीं करेगा। – gabrielhilal

+0

क्षमा करें, मैं खुद को स्पष्ट नहीं कर रहा हूं। मेरे पास केवल एक मॉडल है, Project.rb। मैं अपना कंट्रोलर, सर्च व्यू, रूट्स, मॉडल फाइल अपलोड करूंगा ताकि आपको जो करने की कोशिश कर रहा है उसकी एक बेहतर तस्वीर मिल जाए। – Jazz

1

मैं अपने आवेदन में MetaSearch का उपयोग कर रहा हूं और इसे काफी सुविधाजनक पाया। यदि आप इसे पहले से ही मान चुके हैं, तो आपके पास क्या समस्याएं थीं?

उसी लेखक द्वारा Ransack भी है, यह मेटाशर्च के उत्तराधिकारी है।

0

इसमें एक साधारण स्पष्टीकरण पाया जा सकता है

असल में, हमें परीक्षण करना होगा कि पैरा में एक विशिष्ट फ़ील्ड है और फ़िल्टर बनाएं। नीचे दिए गए उदाहरण देखें:

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 है। रंसैक रेल पर अपने रूबी के लिए सरल और उन्नत खोज फ़ॉर्म दोनों के निर्माण को सक्षम बनाता है

संबंधित मुद्दे