# Using Checkboxes and Ajax to Assign Categories

**URL:** <https://www.ruby-forum.com/t/using-checkboxes-and-ajax-to-assign-categories/125221>\
**Category:** Rails\
**Created:** [January 7, 2008, 6:14am UTC](https://www.ruby-forum.com/t/using-checkboxes-and-ajax-to-assign-categories/125221 "2008-01-07T06:14:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![melvinram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@melvinram](https://www.ruby-forum.com/u/melvinram)\
**Post date:** [January 7, 2008, 6:14am UTC](https://www.ruby-forum.com/t/using-checkboxes-and-ajax-to-assign-categories/125221/1 "2008-01-07T06:14:55Z")

</div>

Hi All,

I have an ‘admin’ controller in which I have a ‘show\_product’ action. In  
the view of this action, I have a list of names of the categories w/  
checkboxes to the left of each.

What I am trying to do is to allow the administrator to just check off  
the categories in this show\_product view and have it add the product to  
the category.

That part I am not able to figure out is how to display the list of  
categories & checkboxes and create observe\_form’s or observe\_field’s to  
send the product\_id and category\_id to the action that will do the logic  
part.

Here is the code I’ve got:

# @categories & @product is available to the show\_product action as

variables

# \_categories.rhtml (partial gets called into the show\_product.rhtml

\<% if @categories %\>  
\<% for category in @categories %\>

 \<%= check\_box :category\_id, :value =\> category.id %\> \<%= label :category, :name, category.name %\> \<%= observe\_form category.id, :frequency =\> 1.0, :update =\> 'category\_notice', :url =\> { :controller =\> 'admin', :action =\> 'update\_product\_category' }, :with =\> "'category=' + encodeURIComponent($('@category.id').value) + '&product=' + encodeURIComponent($('@product').value)" %\> 
 \<% end %\>  
  
\<% else %\> 

No categories found.

\<% end %\>
