Question

I've successfully added ujs in one rails (3.2.6) app. Adding the :remote => true to my form tag allows me to make ajax calls to my js.erb files for dynamic loading of divs, ect..

But in another application on the same machine (Ubuntu 12.0.4), is seems the ujs engine is not working. I'm always getting a Template not Found because the form is sending format => html rather than js. If I force the form to use js format (format => 'js'), it then just renders the js.erb file, rather than calling it via ajax.

In the application.js, I've included the proper headers with the following:

//= require jquery
//= require jquery_ujs
//= require_tree .

The javascript files are included when I actually browse to the primary home page which is using the application layout, which includes the above mentioned javascirpt references. The form in the page is as follows:

<%= form_tag list_path, :remote => true, :id => 'frmBookResults', :method => :post do %>

But although it contains :remote => true, and there's a route established for list_path (the route works, because if I change the list.js.erb to list.html.erb, the view renders), and a method in the controller to handle the request (def list....end), the subsequent list.js.erb is ignored and I get a template not found error, because rails is processing the form request as html, which I can confirm in the log.

I've searched everywhere I could for a solution, but can't figure out why my ujs isn't working for this particular app, when it is nearly identical to my working app, gemset, versions, and configuration.

I've found a couple of other articles on stack overflow where people had the same problem, but no final, working answer was given.

Any help or direction would be greatly appreciated.

Was it helpful?

Solution

It was indeed an issue with the ajax being broken. The onkeyup trigger I was using to submit the form was as follows:

<%= form_tag list_path, :id => 'frmBookResults', :remote => true do %>
    <input id='keyword' type='text' onkeyup='document.forms["frmBookResults"].submit();'/>
<% end %>

Note the following:

document.forms["frmBookResults"].submit();

Apparently, submitting the form via javascript was the issue, because when I updated the onkeyup to use a jquery submit as described below, rails ujs kicked in and the ajax calls to my list.js.erb worked:

onkeyup="jQuery('#frmBookResults').submit()"

Thanks for all the feedback mccanff! Your contribution along with other developers from the rails group at linkedIn helped me finally solve my issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top