标题可能看起来令人困惑但却解释了我想要实施的内容。
我想要的是什么:我希望conatainer-list
包含名为agreement
的{{1}}模型中的所有“类别”,因此每个协议都有一个writing_type by string。这个writing_type
当然会有多个不同的container-list
。默认情况下,将显示所有写入类型。选择一个或多个writing_types
时,列表会将结果过滤到writing_type
个。{
例如
页面加载,列表包含DESC中的所有书写类型和所有行。
容器列表具有以下写入类型“onclick突出显示所选的writing_type蓝色,可以选择多个,或取消选择”
writing_types:{writing_type
,college_essay
,college app
}
我选择foreign language
和college_essay
结果只有大学论文和外语
接下来,我点击名为filter by duedate的按钮
结果仍会包含foreign language
和college_essay
,并会通过duedate DESC进行过滤。
除foreign_language
之外还有许多不同的按钮,例如due_date
或create_at
,但是一次只能选择其中一个辅助过滤器
- 下拉列表旁边是辅助过滤器的按钮
目前这是我现在拥有下拉列表的代码,但它们都是单独过滤的。我不知道如何首先获取price
,然后点击其中一个按钮(例如writing_type
进行过滤。
created_at
的routes.rb
<!-- 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>
agreement.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
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
作为主要结果。谢谢!!!
答案 0 :(得分: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来构建查询并提交它。