How to render the AJAX response in RAILS

前端 未结 1 1850

Ok, pretty simple question, I have working an AJAX method, what it does is that it searches in the database for specific information with a query. I can see in the Console that

相关标签:
1条回答
  • 2021-01-23 17:31

    In your respond_to block put format.js above format.html.

    Make sure you have index.js.erb in your view folder for the controller. This index.js.erb(comes from the action name) should have a jQuery statement like the following if you're using rails 3.1+:

    $('#DOMelementToPlaceContent').html(<%= escape_javascript(render :partial => "partial/location") %>);
    

    This will replace the content of the DOM element with ID DOMelementToPlaceContent with the content in the specified partial.

    Also, you should think about moving the logic for your search to a search action in the same controller in which case you'll need a search.js.erb file in the view folder for the controller.

    0 讨论(0)
提交回复
热议问题