Topic: [SOLVED] Controller "Respond_to" is skipping javascript?

I'm trying to update a DB record using a link.

view file

    <td id="toggle-status">
      <%= render :partial => 'toggle_response', :locals => {:app => appointment} %>
    </td>

_toggle_response.html.erb

<%= link_to 'Toggle Completed', {:controller => 'appointments', :action => 'toggle_completed', :id => app.id}, :id => 'toggle-completed', :method => :put, :remote => true %>

appointments_controller.rb

  def toggle_completed
    @appointment = Appointment.find(params[:id])

    @appointment.completed = @appointment.completed==1 ? 0 : 1
    @appointment.save
    
    respond_to do |format|
      format.js   { render :partial => 'toggle_response', :locals => {:app => @appointment} }
    end
  end

routes

  resources :appointments do
    put 'toggle_completed', :on => :member
  end

and finally, appointments.js.coffee

jQuery ($) ->
  alert 'loaded'
  toggle_status = $("#toggle-status")
  
  setLoadingText = ->
    toggle_status.text "loading..."
  
  $("#toggle-completed").bind("ajax:loading", setLoadingText).bind("ajax:success",
    (data, status, xhr) ->
      alert('DONE')
  )

What happens is, I click the "Set Complete" link and ajax runs like normal. The record DOES GET UPDATED. And the PUT response IS THE PARTIAL. However, the "ajax:success" bind I have in my appointments.js.coffee does not get executed. The script DOES load initially because the "alert 'loaded'" pops up as it should.

The only way I can get my "ajax:success" to execute is if I remove the "format.js" from my controller's respond_to. Why is that?

I've been looking everywhere online, there are no good resources for rails and ajax (as far as updating via a link goes).

I need help on this! I feel like I'm making this harder than it needs to be.

Thanks

Last edited by wesf90 (2012-02-06 23:05:27)

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

Follow me!

Re: [SOLVED] Controller "Respond_to" is skipping javascript?

I've figured it out! Finally, after days and days.

If anyone is looking for the answer, let me know and I'll post it

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

Follow me!

Re: [SOLVED] Controller "Respond_to" is skipping javascript?

If you took the time to post a problem, why didn't you post the answer?

"If anyone is looking for the answer, let me know and I'll post it" is absurd!

Re: [SOLVED] Controller "Respond_to" is skipping javascript?

It requires quite a long explanation. If this question was common enough, I would take the time to explain it. However it is not.

Thanks for digging up a 3 month old post though. It's always nice to see forum cluttered with nonsense.

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

Follow me!

Re: [SOLVED] Controller "Respond_to" is skipping javascript?

wesf90 wrote:

I've figured it out! Finally, after days and days.

If anyone is looking for the answer, let me know and I'll post it

Stuck with a similar issue. I have a link that submits through AJAX, and I have to toggle between alternative functions in JQuery, each time it is clicked. The toggle functions work fine, but the controller action is not called on the first click, rather the second, each time. If I remove the JQuery functions, then the things behaves fine.

Also, if instead of toggle, I use simple click I am able to call both the JQuery function and controller action with the first click itself. This is quite irritating, especially when I seem to be the loner to be having this issue. No clue on the internet, apparently.

Re: [SOLVED] Controller "Respond_to" is skipping javascript?

vandamon wrote:
wesf90 wrote:

I've figured it out! Finally, after days and days.

If anyone is looking for the answer, let me know and I'll post it

Stuck with a similar issue. I have a link that submits through AJAX, and I have to toggle between alternative functions in JQuery, each time it is clicked. The toggle functions work fine, but the controller action is not called on the first click, rather the second, each time. If I remove the JQuery functions, then the things behaves fine.

Also, if instead of toggle, I use simple click I am able to call both the JQuery function and controller action with the first click itself. This is quite irritating, especially when I seem to be the loner to be having this issue. No clue on the internet, apparently.

Okay, got this working with the help of a user defined function, from here. So I can now call both JQuery functions and controller actions with alternative clicks.

Last edited by vandamon (2012-08-29 19:00:40)