Rails 3 - Category Filter Using Select - Load Partial Via Ajax

Posted by fourfour on Stack Overflow See other posts from Stack Overflow or by fourfour
Published on 2010-04-27T08:40:42Z Indexed on 2010/04/27 8:43 UTC
Read the original article Hit count: 474

Filed under:
|
|
|

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-not-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.

© Stack Overflow or respective owner

Related posts about ruby-on-rails3

Related posts about AJAX