Created
March 6, 2011 10:46
-
-
Save aaronvb/857198 to your computer and use it in GitHub Desktop.
Ajax Checkbox Ruby on Rails 2.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://aaronvb.com/blog/2011/3/6/ajax-checkbox-in-ruby-on-rails-2-3/