1

I'm converting our Rails 3 web app to use jQuery mobile, and I'm having problems with "remote" links.

I have the following link:

= link_to "Text", foo_url, :method => :put, :remote => true

Which, on the server, I'm handling like this:

respond_to do |format|
  if foo.save
    format.html { redirect_back_or_to blah_url }
    format.json { render :json => {:status => "ok"} }
  end
end

This used to work wonderfully. However, since I've added jQuery Mobile, the controller code goes through the "html" branch instead of the "json" one, and responds with a redirect.

I've tried adding

 :data => { :ajax => "false" }

to the link, but I get the same effect.

Before jQuery Mobile, UJS was sending the request with the following accept header:

Accept:application/json, text/javascript, */*; q=0.01

while with jQuery Mobile, I'm getting this header:

Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript

I believe this change in headers is the culprit of the change in server-side behaviour. I haven't been able to debug through the client side to figure out who's doing what exactly. UJS is clearly still doing something, since I'm getting a "PUT request" of sorts, things get routed appropriately, etc, but I'm not sure what's changing the headers.

Thank you!
Daniel

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243

1 Answers1

0

By default remote: true goes to the format.js clause (and searches for some .js.erb template to send back), and defaults to format.html and sends back the html template.

You should use ”data-type” => :json in your link_to call if you want to return json, like:

<%= link_to 'Show Full Article', @article, :remote => true, "data-type" => :json %>

Source: http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs

Henry Mazza
  • 764
  • 1
  • 6
  • 18