Rendering json in ruby on rails views - ruby-on-rails

I am in the process of learning ruby on rails,
I have a controller named welcome_controller and the controller has a json object, and I have a view named index.html.haml
How do I access that json object from the view
render 'index', :layout => false

Try this
render json: #payment.to_json

var my_var = JSON.parse(('<%= my_controller_json %>')
console.log(JSON.stringify(my_var))
Put this in of your index page
for more info see this question

Try like this,
render json: #your_json_object

Related

render_to_string doesn't find my erb template?

I am trying to use render_to_string like this:
html_result = render_to_string(:template => 'template_160x600', layout: 'art_layout')
And I keep getting the following error:
Missing template /template_160x600
However, if I render normally, I don't have any problem:
render 'template_160x600', layout: 'art_layout'
What am I missing here?
EDITED:
I have added the view directory like this:
render 'arts/template_160x600', layout: 'art_layout'
But now I just get:
Missing template arts/show, application/show with
I believe that what is happening is that even though you are using render_to_string, Rails tries to do the default render for the show view afterwards.
So, if after render_to_string you don't want to render any view, just do this:
html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')
head :ok
or
html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')
render nothing: true
When you render a template you must specify the path from the root of the template search paths (app/views for one), so if your template is in app/views/art you would use:
html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')

How to create a Rails model without an HTML view

I have a model that I only want to ever return JSON, regardless of any conneg or file-like extensions on the URI (e.g. /app/model.json). Google-fu is coming up short and this can't be that difficult.
In your controllers you simple have to create a respond_to block that only responds to JSON:
respond_to do |format|
format.json { render :json => #model }
end
This is actually a decision made by the controller, not because there is/is not a model or view present. In your controller you can:
render json: #your_model
However you will quickly find that the default implementation of to_json (which is what is used internally, above) can be annoying hard to do exactly what you want. When you reach that point you can use RABL to create views that massage the JSON from your model(s) exactly as you want.

Rendering just html with Rabl

I'm trying to figure out how to render just html with Rabl. I have an html partial without an object, so far all I can see are examples of partials with objects. If I try it without the object it just (obviously) throws an error.
The closest I got was:
node(:content) do
partial("api/v1/api/partials/tips")
end
Here is the documentation for Rabl.
UPDATE
I ended up just going with this below. Obvious right? For some reason when I tried it the first time it didn't work. So for sending only HTML in API responses, this works well.
def tips
render partial: "api/v1/api/partials/tips"
end
in a rabl view, for example show.v1.rabl you're able to render a view partial with the following (rails 4.1.2):
object #your_object
code :html do
context_scope.controller.render_to_string(
# from app/views/
partial: 'path/to/show.html.erb',
# locals (if needed)
locals: {
your_object: #your_object
}
)
end
This can be achieved by rendering an ERB template inside the Rabl node:
object #user
node(:html_content) do |user|
#user = user
template = Rails.root.to_s + '/app/views/api/users/show.html.erb'
ERB.new(File.read(template)).result(self.binding)
end

Rails render html into a hash

Is it possible to have rails render html into a hash instead of to the client?
Something like this:
#obj = {
"foo" => Bar.find(1)
"html" => (render :partial => "yatzhee")
}
render :json => #obj.to_json
render_to_string takes all the same arguments as render but returns a string. You can then put that in a hash or do anything you want with that.
I never saw anything like that.
If you want to render json, you should really take a look at jbuilder. It's a joy to work with and it's the 'rails way' (~ it's merely built-in).
It allows you to render partials, for instance:
json.partial! "api/comments/comments", #message.comments

Is there an easy way to use a JSON layout in Rails 3.1 for `render json: {}`?

Is it possible to specify a layout for rendering JSON in Rails 3.1+ (not that I found any easy way to do it in any previous version of Rails)?
I've been using a helper like this:
def render_as_json(obj, status = 200, *args)
render(inline: obj.to_json(*args), layout: 'default', status: status)
end
It doesn't seem like render json: obj will render a layout.
I just wanted to have some metadata in the layout file:
<%- #content = yield -%>
{
"data":<%= #content.present? ? raw(#content) : '{}' %>,
"metadata":<%= raw(json_layout_metadata.to_json) %>
}
I know this is old now but I was trying to use a JSON layout and an XML layout and while searching I found your question and it helped me somehow solve my problem so here is what I did for the JSON part and the XML is pretty much the same:
Create a template file in the layout folder in your application(in the same directory of application.html.erb), call it anything but make its extension ".json.erb", let's assume you named it "json_layout.json.erb". Put in it whatever constant content you want to add and in the place you want to display dynamic data put a
<%= yield %>
In my case, this is my template:
{"status": <%= response.status %>,"data": <%= yield %>}
Now in your controller where you want to display JSON use the following format in the respond_to block:
format.json { render :inline => #the_object.to_json(any_additional_options) , :layout => "json_layout.json.erb" }
Just be careful in case of XML to put in the options of the to_xml method
:skip_instruct => true
as you would probably don't need the full XML generated by default because you add your own in the layout.

Resources