Question : Rails: Ajax checkbox

I am generating a list from a collection using partials.  In each row of the list I am using a checkbox which the user can use to set the "on-off" state of that item.  The intent is when the user clicks the checkbox an AJAX call is made to set that value in the Database.  I tried to use the "observe_field" but all I got was a JavaScript error:

Value 0 (result of expression this.callback) is not object.
http://localhost:3000/javascripts/prototype.js?1203866909 (line 2140)

Any thoughts as to what I am doing wrong?  Do I need to put the checkboxes within a form?

Thanks,
Grant
Code Snippet:
1:
<%= observe_field("tag_id", :url => {:action => 'updateActive'}, :frequency => 0, :with => "'value='+ escape(value)") %>
Open in New Window Select All

Answer : Rails: Ajax checkbox

Let me know if you have any questions:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
# Here's a quick test I made to demonstrate:
 
# Template:
Update me: <%= check_box_tag 'update_me' %>
<%= observe_field 'update_me', :url => { :action => "update_checkbox" }, :with => 'checked'  %>
 
# This makes a field with an id of "update_me" and creates the observer
# that will be called when it's checked
# The :with option means our parameter will be called "checked"
 
# Clicking the box shows this in the log:
 
Parameters: {"checked"=>"1", ....
Parameters: {"checked"=>"null", ....
 
# So, our box will either be null or 1 (they're both strings, though!)
# Here's the controller code to make sure things are working:
 
  def update_checkbox
    @checked = '1' == params[:checked]
    render :update do |page|
      page.alert "Checked value:  #{@checked}"
    end
  end
Open in New Window Select All
Random Solutions  
 
programming4us programming4us