Chris
Chris

Reputation: 961

Ajax response not executing (javascript)

I have a remote form, and it submits to the search action. Inside of content#search, I have the following:

respond_to do |format|
  #format.html {}
  format.js {}
end

I have a search.js.erb file with the following inside of it:

$('#ajaxstuffhere').html("<%= escape_javascript(render :partial => 'search') %>");

Unfortunately this doesn't modify the div in the main page at all.

When I un-comment the format.html {}, it updates the div with search.html.erb content. Unfortunately this renders with the controller's default layout.

What I want is to render this content without the layout. Ideally using javascript, so I can update some Google maps markers along the way.

This form code:

<%= form_tag({:action => 'search'},:id => 'searchForm', :remote => true) do %>

generates this output:

<form accept-charset="UTF-8" action="/search" data-remote="true" id="searchForm" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>

Can anyone tell what I'm missing?

Edit: I was thinking ... and if the partial is returned as HTML, I'll be hitting the javascript to catch the response anyways, so I can update the google map in there. Either way is good, so long as the layout is gone (I'd still prefer the js.erb way though).

The server log for the javascript response says the following:

 Rendered content/_search.html.erb (270.5ms)
 Rendered content/search.js.erb within layouts/content (274.1ms)

But the page doesn't update at all. It seems to be hitting the javascript and the partial seems to be getting called

Upvotes: 0

Views: 1301

Answers (1)

Oliver
Oliver

Reputation: 391

Make sure a an element with id ajaxstuffhere exists on page. Place the HTML you want rendered inside _search.html.erb. Then render without a layout:

format.js { render layout: false }

Try the chrome developer tools to debug any issues. Use the network tab to see the actual response and the console will flag up any js errors.

An alternative approach would be to render the html partial as the Ajax response and bind to the Ajax success action of your search form.

Upvotes: 3

Related Questions