Topic: Simply update a table with AJAX. How?

I'm wanting to update a DB table by simply clicking a link.

I want to be able to click a link "Toggle" and have it perform the following function:

#appointment_controller.rb
  def toggle_completed
    app_edit = Appointment.find(params[:id])
    
    if app_edit.completed == 0
      app_edit.completed = 1
    else
      app_edit.completed = 0
    end
    
    app_edit.save
  end

Currently, I am using the following code to generate the AJAX link:

#index.html.erb
<%= link_to '(Toggle)', {:controller => 'appointments', :action => 'update', :id => appointment.id}, :remote => true %>

Obviously it doesn't work, but I'm not sure what to do? I tried changing the :action to 'toggle_completed', but that requires me to update my routes. It's an extra step I would like to avoid, but is that the only way to do what I want it to do?

Thanks!

Last edited by wesf90 (2012-01-29 21:31:53)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Simply update a table with AJAX. How?

UPDATE

I've got the "link_to" helper to work like this:

<%= link_to 'Complete', appointment_path(appointment, :appointment => {:completed => 1}), :method => :put, :remote => true %>

That successfully updates my appointment's "completed" column to 1.

However, I'm wanting to be able to run the "toggle_completed" function (mentioned in the first post). So how can I do that?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!