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')
Related
I want to pass a hash to the render method. In my console everything works fine when I do this:
#object = Object.find(params[:id])
#hash_object = #object.to_liquid
#template = Liquid::Template.parse("Welcome to {{object_title}}")
#template.render('object_title' => #hash_object["title"])
But when i want to render the page through my controller and pass it through an app-proxy which expects vanilla liquid, the hash-key isn't appearing. But it's interpreted, because the view shows blank space. If it wouldn't work at all, the view would show:"Welcome to {{object_title}}" or don't even load because of syntax errors.
I tried almost every possible way to render the template. The next two tryouts are the ones which do not throw an error, but show only a blank where the title should appear:
#pageview = Liquid::Template.parse(File.read(Rails.root + "app/views/app_proxy/show.html.erb"))
render text: #pageview.render('object_title' => #hash_object["title"]), content_type: "application/liquid"
And the second one (which I think is a bit cleaner and more rubylike):
respond_to do |format|
format.html do
render text: #pageview.render('object_title' => #hash_object["title"]), layout: false, content_type: "application/liquid"
end
end
Where is the mistake in these renderings or what am I missing?
# variables are assigned to an instance of a class, not the class itself, so you need to declare it an "initialize" method (or "setup" method, if a test)
render html: instead of render text:
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
I have created a haml-partial which I want to use as a default header. When rendering this out I optionally want to give it a block to replace a part of the default content.
I've tried multiple things:
render partial: "partial", capture: do
# Don't really know why I tried this, Syntax error ofcourse.
render partial: "partial" do
# 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
render layout: "partial" do
# Works, but:
render layout: "partial"
# You invoked render but did not give any of :partial, :template, :inline, :file or :text option.
# So, it always needs the block
Any other options I don't know of?
Use render layout: "" when you need a block.
Use render partial: "" when you don't use a block.
They both work with locals and look for a pre-underscored file.
I have an action that needs to render a view to string. The view is called index.xml.erb. I am trying to achieve this with render_to_string:
my_string = render_to_string(layout: false, format: "xml")
render_to_string is instead rendering the contents of index.html.erb and assigning it to my_string. What am I missing?
Note: I am aware that I can do something like this:
my_string = render_to_string(:action => "#{self.action_name}.xml.erb")
But I'm curious as to why the "format" option isn't honored with render_to_string.
This works for me.
render_to_string( :action => "#{self.action_name}", :formats => [:xml] )
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.