简体   繁体   中英

How to get results from my Rails 3 controller with javascript and passed in parameters?

I'm trying to get some results from my controller based one some javascript variables but I haven't the foggiest idea on how to do this.

So far I have:

   $.ajax({
        url: '/shops?state=' + state + '&city=' + city , type: 'get', dataType: 'html',
        complete: function(request){
         $("#shop_results").html("<%= escape_javascript(render :partial => 'shop_results') %>");
        }
    });

Which according to my rails console IS hitting my controller and sending in the parameters but my controller:

  def shops
    @maps = Map.where("state LIKE ?", "#{params[:state]}")
    respond_to do |format|
      format.html
      format.js
    end
  end

my view:

  <div id="map_canvas" style="width: 980px; height: 400px"></div>

  <div id="shop_results">
    <%= render :partial =>"shop_results"%>
  </div>

and the shop_results partial has the code displaying the array:

  <ol class="shop_list" style="background:blue;">
    <% @maps.each do |m| %>
      <li><% link_to m.shop_name ,"#" %></li>
    <% end %>
  </ol>

Butttt nothing is showing up in my view where the partial is called

Not exactly sure what you mean by "it's not returning anything in the console", but if you're doing a "like" query, wouldn't you want some % signs in there? Are you attempting to do state name completion?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM