יום שישי, 12 ביולי 2013

Continue the Searching in Rails

Update the view
Add search form and handle the submit using jquery

  1: <%=form_for :searchP ,:html => { :id => "Start_Search_Form" } do |f| %>
  2:   <fieldset>
  3:     <legend>Search for operator</legend>
  4:     <%= f.label :pharse %>
  5:     <%= f.text_field :pharse,:size=>"45",:class=>"round",:id=>"pharse" %><br/>      
  6:  <p><%= button_to "Start Search",{:class=>"SearchButton"} %></p> 
  7:  </fieldset>
  8:  
  9: <%end%>
 10: 
 11: <script src="../../Scripts/jquery-1.8.2.js"></script>
 12: <script>
 13: 
 14: $('#Start_Search_Form').submit(function() {  
 15:     var valuesToSubmit = $(this).serialize();
 16: 
 17:   alert (valuesToSubmit);
 18:   
 19:     $.ajax({
 20:         url: $(this).attr('StartSearch'), //sumbits it to the given url of the form
 21:         data: valuesToSubmit,
 22:         dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
 23:     }).success(function(json){
 24:        alert (json);
 25:     });
 26:     return false; // prevents normal behaviour
 27: });
 28: 
 29: </script>

note the changing of the rails form id


and the result:
Capture49 


Resources


http://stackoverflow.com/questions/6723334/submit-form-in-rails-3-in-an-ajax-way-with-jquery
http://stackoverflow.com/questions/8682076/change-html-form-id-generated-by-form-for-rails-3-1

אין תגובות:

הוסף רשומת תגובה