1

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?

1
  • It looks like you didn't specify the format in the request Commented Sep 6, 2013 at 19:54

2 Answers 2

18

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
Sign up to request clarification or add additional context in comments.

1 Comment

Check the Accept headers being sent by the browser. If the Accept header has "/" (i.e any format is Ok), then the first format block in respond_to will trigger. If you want to ensure the the .js block is invoked, then one way is to ensure the Accept header has 'text/javascript'. You can set accept headers when sending any ajax request via jQuery or any framework.
0

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

1 Comment

still doesn't explain why it works without the mandatory callback, when you have the format.js first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.