Trying to do the following
#message = render_to_string ( :sender => sender, :template => "template" )
But when accessing #sender in template it turns out to be nil:NilClass. Double checked if I pass the right variable and it's totally fine. Maybe there are other way to pass variables to render_to_string?
It might be the syntax you're using. Try using the :locals argument:
#m = render_to_string :template => "template", :locals => {:sender => sender}
Then you just need to access sender (without an #) as a local variable inside the template.
Here's Jason Kim's solution he wrote in a comment which worked for me:
ActionController::Base.new.render_to_string(
"user_mailer/welcome_email.html.erb", locals: { :#user => user}
)
Please mind the :#user => value bit.
In Rails 5 (atm in beta):
ApplicationController.render(
file: 'path',
assigns: { foo: 'bar' }
)
More here
Try this:
ac = ActionController::Base.new()
ac.render_to_string(:partial => 'path to your partial',:locals => {:varable => your variables})
In rails 4.0.2 this worked:
render_to_string(partial: 'path/to/partial', locals: { argument: 'value'}
I was trying to render a different format of partial in render_to_string. The thing which really worked for me was:
render_to_string(:partial => 'partial_file.html', :locals => {:variable => variable}, :format => :html)
where the name of the file was _partial_file.html.erb.
Related
I have some complicated PDF generation logic that requires rendering a view outside of a controller and then passing the HTML into WickedPDF:
ActionView::Base.send(:define_method, :protect_against_forgery?) { false }
av = ActionView::Base.new
av.view_paths = ActionController::Base.view_paths
income_statement_html = av.render :template => "reports/income_statement.pdf.erb", :layout => 'layouts/report.html.erb',
locals: {:#revenue_accounts => revenue_accounts,
:#expense_accounts => expense_accounts,
:#start_date => start_date,
:#end_date => end_date,
:#business => business}
This all works fine on Rails 4 but has stopped working when we upgraded to Rails 5.
All the instance variables we are setting here end up as nil inside the view. Is there still a way to set instance variables from the render call like this?
Rails 5 introduced ActionController::Base.render, which allows you to do this instead:
rendered_html = ApplicationController.render(
template: 'reports/income_statement',
layout: 'report',
assigns: {
revenue_accounts: revenue_accounts,
expense_accounts: expense_accounts,
start_date: start_date,
end_date: end_date,
business: business
}
)
Which you can then pass to WickedPDF:
WickedPdf.new.pdf_from_string(rendered_html)
You can read more about .render and using it with WickedPDF, as well get some examples of how to extract this functionality into reusable objects on this blog post.
ActionView::Base has a method assign which can be called to set the instance variables.
av.assign({revenue_accounts: revenue_accounts,
expense_accounts: expense_accounts,
start_date: start_date,
end_date: end_date,
business: business})
income_statement_html = av.render :template => "reports/income_statement.pdf.erb", :layout => 'layouts/report.html.erb'
I am using TinyMCE and I have rolled my own spellchecker using FFI-Hunspell.
I am just rendering this hardcoded response but when I click the spell check button in the WYSIWYG editor, it says that there aren't any misspelled words.
render :json => {:id => "#{params[:id]}", :result => {"presents" => ["presnts"], "motor" => ["moors"]}}.to_json
So, what is the JSON supposed to look like?
I am using the tinymce_rails gem. I would have thought it was using the newer version. Anyways, I found this link that describes in detail how the request/response should look: https://github.com/spohlenz/tinymce-rails. Effectively, the response for the older version of tinyMCE is this:
render :json => ({:id => nil, :result => ['badk', 'wirds'], :error => nil}).to_json
Also, it actually uses a second request to get the suggestions. And those should look like:
render :json => ({:id => nil, :result => ['bad', 'bed'], :error => nil}).to_json
I currently have some code to render a partial, which includes:
:locals => {:feature => y}
This works fine, but I would like to be able to use a variable in place of :feature - I have tried the following:
:locals => {type.singularize => y}
but to no avail. What is the correct syntax to replace :feature with a local variable?
Incase anyone else comes across this, I was able to resolve it using the code in the comments from apneadiving and MurifoX:
:locals => {type.singularize.to_sym => y}
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] )
So... I know about the :locals => { :var1 => #rawr, :var2 => #hello } syntax for partials
but is there a way for me to pass both #rawr and #hello to the partial, so that i don't need to use var1 and var2?
You can do :locals => { :rawr => #rawr, :hello => #hello } and then the variables will be available within the partial as rawr and hello.
You know, you could just use #rawr and #hello ... and NOT pass any variables.