Rendering result in desire format making ajax call to local method in Rhomobile

While developing application, we sometimes need to make ajax call to local methods so that we can render the updated data without refreshing the whole page. We can achieve this using RhoMobile. If we need, we can call local method and get the data using JavaScript ajax call and finally render them into the current view without reloading it.

We can get desired format of output, using the advance use of ‘render’. If we need to render some data in different format like STRING, JSON, XML, following are the different ways to do the job.

Method 1 : Render as String
def get_string
render :string => ‘This is a sample line’

Method 2 : Render as Json
def get_json
@response[“headers”][“Content-Type”] = “application/json; charset=utf-8”
render :string => ‘{“a”: 1, “b”: 2, “c”: 3}’

Method 3 : Render as Xml
def get_xml
@response[“headers”][“Content-Type”] = “application/xml; charset=utf-8”
render :string => ‘<note><heading>Reminder</heading><body>Don’t forget me this weekend!</body></note>’

We also need to handle the :layout during the AsyncHTTP/Javascript Ajax call on above methods so that the current layout do not get rendered during the call. This can be done by the following way,
def get_json
@response[“headers”][“Content-Type”] = “application/json; charset=utf-8”
render :string => ‘{“a”: 1, “b”: 2, “c”: 3}’, :layout => false

So using the above method we can get desired output that may be STRING or JSON or XML, and parse them as required.

Written ByAshis Kumar, Software Developer, Mindfire Solutions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s