renatojf
renatojf

Reputation: 744

JQuery posting form via AJAX not triggering rails respond_to format.js

I have a simple form with an order by param. When my dropdown is changed, I call this:

$.post("/busca", $("#order_form").serialize(), dataType: "script")

On the rails controller side I have a simple format.js to handle the ajax calls. Thing is that this isn't working. The js.erb template is never rendered.

My log shows Processing by BuscaController#index as */* and I have no idea what the */* stands for. Can someone help me?

Upvotes: 1

Views: 4906

Answers (2)

user2293394
user2293394

Reputation: 1

Seems that all post parameters are mandatory:

jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

I was able to get the js when doing this:

$.post("/busca", $("#order_form").serialize(), function() { }, "script")

No matter the format.x order

Upvotes: 0

renatojf
renatojf

Reputation: 744

Ok so I figured it out. When you have a respond_to block like this:

respond_to do |format|
  format.html
  format.js
end

It will not work. You need to setup the js response first than the HTML one. Don't ask me why. This is the one that works for me:

respond_to do |format|
  format.js
  format.html
end

Upvotes: 18

Related Questions