Skip to content

Instantly share code, notes, and snippets.

@aaronvb
Created March 6, 2011 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronvb/857198 to your computer and use it in GitHub Desktop.
Save aaronvb/857198 to your computer and use it in GitHub Desktop.
Ajax Checkbox Ruby on Rails 2.3
# Note model has a column, :important, :boolean, :default => false
# This goes in your HTML erb
<% @user.notes.find(:all, :order => 'updated_at DESC').each do |user_note| %>
<% remote_form_for user_note, {:url =>
remote_toggle_important_user_note_path(:user_id => @user.id, :id => user_note.id)} do |f| %>
<%= f.check_box :important, :onclick => "$('edit_user_note_#{user_note.id}').onsubmit()" %><%= f.label :important %>
<% end %>
<% end %>
# routes
map.resources :users do |user|
user.resources :notes, :member => { :remote_toggle_important => :put }
end
# notes controller
def remote_toggle_important
@note = UserNote.find_by_id(params[:id])
if @note
@note.update_attributes(params[:user_note])
end
end
@aaronvb
Copy link
Author

aaronvb commented Mar 9, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment