Render :json in RhoMobile?Gerbrand Stap Jun 7, 2012 7:58 AM
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.
Re: Render :json in RhoMobile?Arquimidez Mora Jun 7, 2012 8:10 AM (in response to Gerbrand Stap)
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
Re: Render :json in RhoMobile?Gerbrand Stap Jun 7, 2012 8:27 AM (in response to Arquimidez Mora)
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.
Re: Render :json in RhoMobile?Lobby Jones Jun 7, 2012 9:20 AM (in response to Gerbrand Stap)
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.
Re: Render :json in RhoMobile?Gerbrand Stap Jun 7, 2012 11:33 PM (in response to Lobby Jones)
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.
Re: Render :json in RhoMobile?Evgeny Vovchenko Jun 8, 2012 12:18 AM (in response to Gerbrand Stap)
Yes, it does not supported.
You can parse json to ruby variable:
parsed = Rho::JSON.parse("[{\"count\":10}]")
And insert ‘parsed’ to erb-page
JSON library support
Re: Render :json in RhoMobile?Gerbrand Stap Jun 8, 2012 5:05 AM (in response to Evgeny Vovchenko)
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.
Re: Render :json in RhoMobile?Dmitry Prokhorov Jun 9, 2012 1:14 AM (in response to Gerbrand Stap)
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
Re: Render :json in RhoMobile?Gerbrand Stap Jun 11, 2012 12:13 AM (in response to Dmitry Prokhorov)
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.
Re: Render :json in RhoMobile?Evgeny Vovchenko Jun 8, 2012 8:26 AM (in response to Gerbrand Stap)
Could you please to describe use case in more details?
Re: Render :json in RhoMobile?Gerbrand Stap Jun 11, 2012 1:27 AM (in response to Evgeny Vovchenko)
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.
Re: Render :json in RhoMobile?Arquimidez Mora Jun 11, 2012 10:11 AM (in response to Gerbrand Stap)
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
Re: Render :json in RhoMobile?DecisionPoint Keith Gable May 14, 2013 12:06 PM (in response to Gerbrand Stap)
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:
Re: Render :json in RhoMobile?Gerbrand Stap May 21, 2013 12:27 AM (in response to DecisionPoint Keith Gable)
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.
Re: Render :json in RhoMobile?DecisionPoint Keith Gable May 21, 2013 12:31 AM (in response to Gerbrand Stap)1 of 1 people found this helpful
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.
Re: Render :json in RhoMobile?Dariusz Gardynski Jul 19, 2016 3:21 AM (in response to DecisionPoint Keith Gable)
In 5.4 it doesn't work anymore. This work fine - Ruby on Rhodes using ajax call - Stack Overflow.
Re: Render :json in RhoMobile?25c49980-cb2e-4113-a5a7-2f0078c85330 Aug 31, 2012 7:31 AM (in response to Gerbrand Stap)
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
Re: Render :json in RhoMobile?Steve Zhang Oct 15, 2012 6:31 AM (in response to 25c49980-cb2e-4113-a5a7-2f0078c85330)
It does work!
I found you don't need @response statement
Nice job!
Thanks a lot!