Tuesday 15 March 2011

How to render json and at the same time refresh a partial using AJAX (Rails) -


I have a controller action (this is called some_controller_action) to get some JSN (to take Javascript) and at the same time Partial refresh / re-render using some_controller_action.js.erb

My code suggestion is something like this:

  def some_controller_action # # Too many codes here # response_to do | Format | Render: Jason = & gt; {: Some = & gt; Some},: position = & gt; : Ok format.html {redirect_to from somewhere} format.js End End   

Client side Ajax / JavaScript triggers this action and receives Jason as a response. It looks like:

  some CoffeescripAction = - & gt; $ .jax type: "post" datatype: "jason" data: some_data_name: some_data_content url: "some_url / some_controller_action" success: (data, status, xhr) - & gt; Do_something_with_the_response (data) error: (xhr, textStatus, errorThrown) - & gt;   

Of course this does not work, because I do not have permission to present it twice in one action. But then what to do?

What does your JavaScript look like? Do you receive the Administrator's action by AJAX? If so, all you have to do is wait till the AJAX call, and then trigger your partial re-order, either by simply using the received JSN data or by calling any other AJAX function.

No comments:

Post a Comment