views:

519

answers:

1

Hey. I am trying to filter Client Comments by using a select and rendering it in a partial. Right now the partial loads @client.comments. I have a Category model with a Categorizations join. This all works, just need to know how to get the select to call the filter action and load the partial with ajax. Thanks for you help.

Categories controller:

def filter_category
    @categories = Category.all

    respond_to do |format|
      format.js # filter.rjs
    end    
end

filter.js.erb:

page.replace_html 'client-note-inner', 
                  :partial => 'comments', 
                  :locals => { :com => Category.first.comments }

show.html.erb (clients)

<% form_tag(filter_category_path(:id), :method => :put, :class => 'categories', :remote => true, :controller => 'categoires', :action => 'filter') do %>
    <label>Categories</label>
    <%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, @category_id)) %>
<% end %>

<div class="client-note-inner">
    <%= render :partial => 'comments', :locals => { :com => @comments } %>
</div><!--end client-note-inner-->

Hope that makes sense. Cheers.

A: 

It's straightforward with a simple onchange

<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, @category_id), onchange => 'form.submit()') %>
Thomas R. Koll