On the main page, I have a list of videos and pictures. Those videos & pictures belongs to a category. So, I have a filter for the category and one for the type(vidéos or pictures). The problem is that I can't use both filter at the same time without altering the first one. If for example I want to show only the picture and then I want a specific category, I get all pictures and videos from the category selected.
Exemple with a filter of pictures :
view/galeries/index.html.erb
<%= link_to show_pictures_path, :remote => true, :id => "btn-pictures", :class => "btn btn-default" do %>
<em class="glyphicon glyphicon-picture"></em> Pictures
<% end %>
controller/galeries_controller.rb
def show_pictures
@selected = Picture.order('created_at DESC')
end
view/galeries/show_pictures.js.erb
$("#media_filter").html("<%= escape_javascript(render partial: 'partials/media_filter', locals: { medias: @selected } ) %>");
EDIT - Exemple for categories :
view/galeries/index.html.erb
<% @categories.each do |c| %>
<li class="" id=btn-filter<%=c.id%>>
<%= link_to show_categories_path(:id => c.id), :remote => true do %>
<span class="badge pull-right"><%= Media.joins(:pictures).where(category_id: c.id).count + Media.joins(:videos).where(category_id: c.id).count %></span> <%= c.name %>
<% end %>
</li>
<% end %>
controller/galeries_controller.rb
def show_categories
@videos = Video.joins(:media).where("medium.category_id = ?", params[:id])
@pictures = Picture.joins(:media).where("medium.category_id = ?", params[:id])
end
view/galeries/show_categories.js.erb
$("#media_filter").html("<%= escape_javascript(render partial: 'partials/media_filter', locals: { medias: @videos} ) %>");
$("#media_filter").append("<%= escape_javascript(render partial: 'partials/media_filter', locals: { medias: @pictures } ) %>");