I just installed a rails 3.0.3 project on a new machine and I’m getting some weird behavior related to the form_tag :remote => true. It seems the wrong Accept header is set in the request, so it doesn’t come in as an AJAX/JS request, but rather an HTML request. On my production server the Accept header is set correctly:
Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
Specs:
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]
Rails 3.0.3
However, on the new machine I’m setting up it looks like this:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Specs:
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
Rails 3.0.3
This is the code for generating the form (inside _request.html.erb)
<div id='request-translation'>
<h2 class="tr-req left"><%= t( 'user.translation_requests.request' ) %></h2>
<%= form_tag( request_translation_artist_song_path( @artist, @song ),
:method => :post,
:remote => true,
:class => 'left',
:style => 'margin-left: 20px;' ) %>
<%= languages_combobox_large( @into_languages, 'language_id' ).html_safe %>
</form>
</div>
And of course in my controller I only respond to format.js, but the request comes in as format.html. I thought :remote => true took care of this. The generated HTML for the form looks good as well:
<form accept-charset="UTF-8" action="/xxx/request_translation" class="left" data-remote="true" format="js" method="post" style="margin-left: 20px">
Please ensure you are including jquery.js as well as *jquery_ujs.js* in your HTML header.
jquery_ujs is responsible for sending forms via AJAX that have set the data-remote attribute. As it is unobtrusive the form will work without that as well. But it is sent via normal page request.