Rails - how to render a JSON object in a view?

I would recommend that you write that code in an helper itself. Then just use the . To_json method on the array.

Up vote 2 down vote favorite share g+ share fb share tw.

Right now I'm creating an array and using: render :json => @comments This would be fine for a simple JSON object, but right now my JSON object requires several helpers which is breaking everything and requiring helper includes in the controller which seems to cause more problems than solved. So, how can I create this JSON object in a view, where I don't have to worry about doing anything or breaking anything when using a helper. Right now the way I'm making the JSON object in the controller looks little something like this?

Help me migrate it to a view :) # Build the JSON Search Normalized Object @comments = Array. New @conversation_comments. Each do |comment| @comments comment.

Id, :level => comment. Level, :content => html_format(comment. Content), :parent_id => comment.

Parent_id, :user_id => comment. User_id, :created_at => comment. Created_at } end render :json => @comments Thanks!

Ruby-on-rails json ruby-on-rails-3 actionview link|improve this question asked Mar 1 '11 at 23:14AnApprentice5,856446165 70% accept rate.

– macarthy Mar 1 '11 at 23:19 the html_format is a helper that users simple_format and auto_link. That's where all the trouble is. – AnApprentice Mar 1 '11 at 23:22 1 Follow these guidelines – Vicente Reig Mar 1 '11 at 23:29.

I would recommend that you write that code in an helper itself. Then just use the . To_json method on the array.

# application_helper. Rb def comments_as_json(comments) comments. Collect do |comment| { :id => comment.

Id, :level => comment. Level, :content => html_format(comment. Content), :parent_id => comment.

Parent_id, :user_id => comment. User_id, :created_at => comment. Created_at } end.

To_json end # your_view.html.erb.

– AnApprentice Mar 1 '11 at 23:22 I assume you found that answer yourself by now - but yes, you don't need to explicitly include other helpers within a helper. Note: This requires that your ApplicationController has helper :all (default). – Marcel Jackwerth Mar 2 '11 at 0:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions