0

I'm trying to write an AJAX call that renders a new partial when a particular item is selected from a drop down list. I have been searching for a solution all day with no luck :(

The only way I got it working was not using AJAX, but having a hidden div containing my second partial, and then showing it and hiding the other div when the appropriate option is selected from the drop down box. Yes this worked but I was looking for a cleaner solution.

I'm not looking for a direct answer, but if anyone has any resources that could point me in the right direction it would be greatly appreciated :)

4

2 回答 2

1

How about adding a controller action that would render the view (so it's view would just render the partial), and then using jquery to place it:

$('#id-of-box').load("<%= some_action_path %>")
于 2012-05-14T07:57:42.193 回答
0

You should perform a remote AJAX call when the onchange event from your select element is being triggered, so on the page which contains your select element there should be something like this (inside a script):

$('#select_id').live('change',function(){
  $.ajax({
    url: 'url_bound_to_partial_renderer'
  });
}

and on the action of the controller which responds to your url_to_fetch_result, you should write something like this:

# controller
# action bound to 'url_bound_to_partial_renderer' by routes.rb
def partial_renderer
   ...
end

#view (partial) _partial_renderer.js.erb
$('jquery_selector_path_to_container_element_of_your_partial').html("<%= escape_javascript render(:partial => 'my_partial') %>");

#view (partial) to be rendered inside container element: _my_partial.html.erb
<p>Here goes the content of the partial you want to be rendered on the container!</p>

Hope this will respond to your question.

于 2012-05-14T09:12:30.393 回答