0

This is my first time trying to implement an Ajax call in rails 3, though I am using the .load function ( I still hope this is Ajax otherwise im understanding this incorrectly)

So i have a search form that returns results via a get request which renders on a different page, i would like the results to appear on the same page as the search form

<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'})  do |select| %>
<%= label_tag :search, "Enter Keywords Here" %>
<%= text_field_tag :search, params[:search] %>
 (I have shortened the form)
<%= submit_tag "Search", :class => "searchbutton" %>
<% end %>

Jquery/Ajax call

$(document).ready(function() {
$('.searchbutton').click(function() {
$('#searchres').load('shared/searchresults');

});

});

View

<h3>Search Recipes here</h3>
  <%= render 'shared/searchrecipes' %>
    <div id ="searchres">
</div>

What am i doing wrong?

4

1 回答 1

1

Due to this being an AJAX call, you need to add remote: true besides method: :get, getting an html parameter hash like this:

{:method => 'get', :remote => true}

When Rails finda remote call, it prevents the default automagically. Otherwise, you would need to modify you javascript like this:

$(document).ready(function() {
  $('.searchbutton').click(function(evt) {
    $('#searchres').load('shared/searchresults');
    evt.preventDefault();
  });
});
于 2012-11-12T20:36:08.393 回答