2017-03-07 126 views
1

標題可能看起來令人困惑,但生病說明我想實現。過濾器列表結果與主過濾器「與多」和次過濾器「只有一個」

我想要什麼:我想conatainer-list包含在agreement模式叫writing_type所有的「類別」,所以每個協議將不得不字符串writing_type。這個container-list當然會有多個不同的writing_types。默認情況下會顯示所有的寫入類型。當選擇一個或多個writing_type的列表將過濾結果到那些writing_type的。

  • 例如

  • 頁面加載和列表包含所有寫入的類型和降序的所有行。 「選擇的onclick亮點writing_type藍色,可以選擇多,或取消選擇」 具有下列文字類型

  • 容器列表

  • writing_types:{college_essaycollege appforeign language}

  • 我選擇college_essayforeign language

  • 結果出來只有大學作文和外語

  • 下一個我的交貨期

  • 單擊名爲過濾器的按鈕,結果仍包含college_essayforeign_language,將由交貨期DESC過濾。

  • 會有許多不同的按鈕除了due_datecreate_atprice,但是僅可以在一個時間

- 現在旁邊的下拉是用於次要的按鈕來選擇這些二次濾波器中的一個過濾器

目前這是我現在使用下拉式代碼,但它們都是單獨過濾。我不知道如何先獲取writing_type,然後點擊其中一個按鈕(如created_at)進行過濾。

enter image description here

<!-- filters for the categories --> 
 
<div class="row text-center" style="margin:0;padding-bottom:10px;"> 
 
<div class="center" style="margin-left:18px;"> 
 
<div style="float:left;"> 
 
<%= link_to "Title", title_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 
<!-- drop down with the writing types listed --> 
 

 
<div class="agreements dropdown" style="float:left;margin-top:40px;"> 
 

 
<%= link_to '#', class:'btn categories-button dropdown-toggle', role:'button', style: 'text-decoration:none', 'aria-expanded' =>'false', data:{toggle: 'dropdown'} do %> 
 
Writing Type 
 
<span class="caret"></span> 
 
<% end %> 
 
<ul class="dropdown-menu" style=""> 
 

 

 
<h>Choose a Writing Type:</h> 
 
<li><%= link_to "College Applications", collegeapplication_agreements_path, class: "link btn categories-button" %></li> 
 
<li><%= link_to "College Essays", collegeessay_agreements_path, class: "link btn categories-button" %></li> 
 
<li><%= link_to "Business Papers", businesspaper_agreements_path, class: "link btn categories-button" %></li> 
 

 
<li><%= link_to "Resumes", resume_agreements_path, class: "link btn categories-button" %></li> 
 
<li><%= link_to "Scholarship Essays", scholarshipessay_agreements_path, class: "link btn categories-button" %></li> 
 
<li><%= link_to "High-School Essays", highschoolessay_agreements_path, class: "link btn categories-button" %></li> 
 
<li><%= link_to "Language Translation", languagetranslation_agreements_path, class: "link btn categories-button" %></li> 
 
</ul> 
 
</div> 
 

 
<div style="float:left;"> 
 
<%= link_to "Recent", recent_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 
<div style="float:left;margin-top:40px;"> 
 
<%= link_to "Oldest", oldest_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 

 

 
<div style="float:left;"> 
 
<%= link_to "Close Duedate", recentduedate_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 
<div style="float:left;margin-top:40px;"> 
 
<%= link_to "Further Duedate", oldestduedate_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 
<div style="float:left;"> 
 
<%= link_to "Lowest Price", lowestprice_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 
<div style="float:left;margin-top:40px;"> 
 
<%= link_to "Highest Price", highestprice_agreements_path, class: "link btn categories-button" %> 
 
</div> 
 

 
</div> 
 

 
</div>

的routes.rb

resources :agreements, path: "agreement" do 
    collection do 
    get :recent 
    get :oldest 
    get :recentduedate 
    get :oldestduedate 
    get :collegeapplication 
    get :collegeessay 
    get :businesspaper 
    get :resume 
    get :scholarshipessay 
    get :highschoolessay 
    get :languagetranslation 
    get :title 
    get :lowestprice 
    get :highestprice 
end 
end 

協議。RB模型

scope :recent, ->{ order("created_at DESC") } 
scope :oldest, ->{ order("created_at ASC") } 
scope :recentduedate, ->{ order("due_date DESC") } 
scope :oldestduedate, ->{ order("due_date ASC") } 
scope :lowestprice, ->{ order("current_price ASC") } 
scope :highestprice, ->{ order("current_price DESC") } 
scope :collegeapplication, ->{ where("writing_type = ?", "College Applications") } 
scope :collegeessay, ->{ where("writing_type = ?", "College Essays") } 
scope :businesspaper, ->{ where("writing_type = ?", "Business Papers") } 
scope :resume, ->{ where("writing_type = ?", "Resumes") } 
scope :scholarshipessay, ->{ where("writing_type = ?", "Scholarship Essays") } 
scope :highschoolessay, ->{ where("writing_type = ?", "High-School Essays") } 
scope :languagetranslation, ->{ where("writing_type = ?", "Language Translation") } 
scope :title, ->{ order("title DESC") } 
def index 
    @agreements = Agreement.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 

end 

def recent 
    @agreements = Agreement.recent.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def oldest 
    @agreements = Agreement.oldest.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def recentduedate 
    @agreements = Agreement.recentduedate.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def oldestduedate 
    @agreements = Agreement.oldestduedate.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def collegeessay 
    @agreements = Agreement.collegeessay.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def resume 
    @agreements = Agreement.resume.where("accepted = ?", false).paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def languagetranslation 
    @agreements = Agreement.languagetranslation.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def collegeapplication 
    @agreements = Agreement.collegeapplication.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def businesspaper 
    @agreements = Agreement.businesspaper.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def scholarshipessay 
    @agreements = Agreement.scholarshipessay.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def highschoolessay 
     @agreements = Agreement.highschoolessay.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
     render action: :index 
    end 



def title 
    @agreements = Agreement.title.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def lowestprice 
    @agreements = Agreement.lowestprice.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

def highestprice 
    @agreements = Agreement.highestprice.where("accepted = ?", false).all.paginate(:page => params[:page], :per_page => 20) 
    render action: :index 
end 

到目前爲止,我已經做這個和它的作品所有的個體,但我想要的篩選結果是基於什麼writing_type被選擇作爲主要結果。謝謝!!!

回答

1

首先,也許使用查詢參數而不是單獨的路徑/操作,因爲它們都使用不同的條件來執行相同的操作。這將顯着簡化代碼。

查看

<%= link_to "Resumes", agreements_path(writing_type: 'Resume', price_asc: true) %> 
<%= link_to "Business Papers Cheapest First", agreements_path(writing_type: 'Business Papers', price_asc: true) %> 
<%= link_to "Business Papers ", agreements_path(writing_type: 'Business Papers', price_desc: true) %> 

第一的link_to上面會成爲 「http://localhost:3000/agreements?writing_type=resume&price_asc=true

然後你就可以在控制器中做到這一點:

def index 
    @agreements = Agreement.where(accepted: false).where(params[:writing_type]).order(sorting) 
end 

private 
    def sorting 
    sort = [] # there is probably a smarter way to do this 
    sort << 'price ASC' if params.has_key? :price_asc 
    sort << 'price DESC' if params.has_key? :price_desc 
    sort << 'duedate ASC' if params.has_key? :duedate_asc 
    sort << 'duedate DESC' if params.has_key? :duedate_desc 
    sort.join ', ' 
    end 

然後,如果你需要多個選擇,你可以使用常規形式或Javascript來建立查詢並提交。

+0

這一切都對我有意義,謝謝你!當我回來確認它的工作時,我會實施! –