Rails Acts as Taggable On
I am using acts-as-taggable-on to tag a Car model. I have three different tag contexts: Make, Color & Features. The first two Make & Color are predefined in an ENV variable and the last context is user generated(automatic windows, heated seats etc.)
What I'd like to do is make it so that from my index page Cars can be filtered by multiple tags at a time. So for instance, I'd like a series of checkboxes at the top of the index page a separate set for each context where a user can check Hyundai & Red and the index of cars would populate with only red Hyundais. At the moment I have a route and some code in my Cars Controller that allows me to filter by one tag but I'd like to modify it to filter by whatever a user selects.
routes.rb
get 'tags/:tags', to: 'cars#index', as: :tag
controller
def index
if params[:keyword].present?
@cars = Car.tagged_with(params[:keyword])
else
@cars = Car.all.order(:cached_weighted_score => :desc)
end
end
The @selected_tag
is tags that have been selected as parameter in index page.
Controller
def index
if params[:keyword].present?
@selected_tag = params[:keyword]
@cars = Car.tagged_with(params[:keyword])
else
@cars = Car.all.order(:cached_weighted_score => :desc)
end
end
View
It's only prototype view so you have to combine with your own code.
<% @tags.each do |tag| %>
<div>
<%= check_box_tag "keywords[]", tag.name, @selected_tag.present? ? @selected_tag.include?(tag.name) : '' %>
<%= tag.name %>
</div>
<% end %>
I hope I can give a general overview with my description. For more information to prepopulate data in checkbox
链接地址: http://www.djcxy.com/p/35946.html上一篇: 红宝石在轨道上
下一篇: Rails表现为Taggable