Render :json in RhoMobile?

According to paragraph 2.2.8 in the Ruby on Rails Guides, rendering json can be done using 'render :json => jsondata'.

This does not work in RhoMobile. The closest thing is 'render :string => jsondata', but this still renders html, because the result is enclosed in <div> tags. Adding ":content_type => 'application/json'" does not seem to make a difference.

How can a controller method be changed to return json data?

I would want to use this to do AJAX calls from the application.

Darryn Campbell
So we're stuck with rendering

So we're stuck with rendering HTML and should strip the <div> tags?

Are there plans to support JSON rendering?

My concerns are that additional tags or attributes may be added in the future, apart from the <div> tags. It just doesn't feel right to get some JSON from HTML code. So I'd rather use functionality that is supposed to yield JSON.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steve Zhang
It does work!I found you don

It does work!

I found you don't need @response statement

Nice job!

Thanks a lot!

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Gerbrand Stap
This does work, but all

This does work, but all special characters are suddenly HTML encoded. This can probably be fixed, so thanks for the suggestion.

But HTML rendering is still used 'under the hood', because of the 'content = super(options)' statement.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


DecisionPoint K...
Actually, yeah that did fix

Actually, yeah that did fix it. When you access a controller action with Ajax, it returns it without the layout. Since you're rendering a string of JSON, you don't get the DIVs anymore. You may also want to set the content-type of the response to remove some of the jQuery boilerplate stuff you need otherwise, but it sounds like you have it working well as is.

Vote: 
Vote up!
Vote down!

Points: 1

You voted ‘up’


Dmitry Prokhorov
It looks like we need to fix

It looks like we need to fix this issue. As a spike solution you can go this way:

1) put this ruby code as app/helpers/no_div_helper.rb

module NoDivHelper
  def render(options = nil)
    options[:string] = options[:json] if (not options.nil?) and options[:json]
    content = super(options)
    content = content.sub(/^<div>/, '').sub(/<\/div>$/, '') if (not options.nil?) and options[:json]
    content   end end def self.new   super.extend NoDivHelper end

2) add this to your controller class (a sample of)

require 'helpers/no_div_helper'

class SomeController < Rho::RhoController
  include NoDivHelper
  ...
  ...
end

3) Use it in action method

  def test_json
    render :json => '{a:1, b:2, c:3}'
  end

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Arquimidez Mora
Hi,I'm working with

Hi,

I'm working with webservices that return and receive json data, and there's no need of render json as "render :json =>",

i just work with that as i said before with:

decode_content = Rho::JSON.parse(json_content)     for parse it and

json_hash = hash.to_json           for encode

and i didn't have any problem, also for the data stored on the database it isn't any work to add

the <%= %> for show the data on the html views

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Darryn Campbell
Unfortunately this does not

Unfortunately this does not work. The controller still wants to render HTML in your approach.

The :json parameter of render seems to be completely ignored. And when the .erb file does not contain <div> tags, they are added.

I'm looking for a way to 'render' JSON in stead of HTML.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Arquimidez Mora
Hi,For decode a json string

Hi,

For decode a json string on Rhomobile just do this:

decode_content = Rho::JSON.parse(json_content)

and for encode content:

hash = {"one" => 1, "two" => 2}

json_hash = hash.to_json

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Lobby Jones
I'm a newb here, but it seems

I'm a newb here, but it seems like this would work:

$myvar = @user.to_json

render :json => :mypage

in mypage.erg

<%= $myvar %>

I'd expect it to give you better control over the HTML output.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Gerbrand Stap
I do not yet have an exact

I do not yet have an exact use case, but I am exploring what the features of a RhoElements v2 app are and which we could use in our application.

I thought it would be useful to get data (for a search for example) from the local database or external webservice in a page. This could be done using json or by rendering the html in ruby and showing that directly. Knowing that rendering json is not supported, we probably will use the latter approach.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


DecisionPoint K...
This response is coming at

This response is coming at you a year later, but here's your code:

def GetUser 

     @user = [some way to get a user] 

     render :json => @user.to_json 

end

To do this in RhoMobile:

def GetUser

     @user = User.find(:first)

     @user_json = make_a_json_object # Can use ::JSON.generate({ :field1 => @user.field1 }); probably you should make a Rhom mixin that provides to_json

     @response["headers"]["Content-Type"] = "application/json; charset=utf-8"

     render :string => @user_json, :layout => false

end

I just wrote a tip on how to set the response content type, which you will likely want to do at some point. Check it out here: https://developer.motorolasolutions.com/thread/3313

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Evgeny Vovchenko
Could you please to describe

Could you please to describe use case in more details?

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


John Hadfield
It looks like the following

It looks like the following is working for me.  I found it on a forum somewhere!

Cheers

John

  def create

    @response['headers']['Content-Type']='application/json'

    result = ::JSON.generate( { :success => 'true', "data" =>

                                                       {"clientId" => "test-100",

                                                        "eventId" => "100" } )

   

    render :string => result, :use_layout_on_ajax => true

end

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Gerbrand Stap
Yes, that I know, but it is

Yes, that I know, but it is not what I want to achieve.

I want to make a controller that might look something like this:

def GetUser

     @user = [some way to get a user]

     render :json => @user.to_json

end

The GetUser method should then return Json only.

When using 'render :string => @user.to_json', I get a result like '<div>{ "name" = "John Doe" }</div>'. But I don't want the div's.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Evgeny Vovchenko
Yes, it does not supported

Yes, it does not supported.

You can parse json to ruby variable:

parsed = Rho::JSON.parse("[{\"count\":10}]")

And insert ‘parsed’ to erb-page

http://docs.rhomobile.com/rhodes/extensions

JSON library support

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Dariusz Gardynski
In 5.4 it doesn't work

In 5.4 it doesn't work anymore. This work fine - Ruby on Rhodes using ajax call - Stack Overflow.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Gerbrand Stap
As time and development of

As time and development of the application progressed, I forgot a little bit about this issue. The funny thing is that we now use json 'rendering' a lot (for data updates) and are no longer experiencing the problem with the <div> tags.

I've never actually figured out why this problem no longer occurred. We've had some updates of RhoStudio/Rhodes, what may have made a change. But I assume that the biggest change is the way we are retrieving the data. I think I did my first tests with a link to the right controller and method, but now all these data updates are done using AJAX calls. Could this have fixed the problem?

If people are interested, I could create a small test app to see what caused the problem.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Topic locked