开发者

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

开发者 https://www.devze.com 2022-12-28 02:13 出处:网络
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

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_sele开发者_高级运维ct(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.


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()') %>
0

精彩评论

暂无评论...
验证码 换一张
取 消