24

I'm trying to use delayed_job to update a remote database via xml

In my lib folder I put a file with a class that should do a render_to_text with template.xml.builder, but I get:

undefined method `render_to_string' for #<SyncJob:0x7faf4e6c0480>...

What am I doing wrong?

5 Answers 5

62
ac = ActionController::Base.new()
ac.render_to_string(:partial => '/path/to/your/template', :locals => {:varable => somevarable})
Sign up to request clarification or add additional context in comments.

4 Comments

If you are trying to use it with instance variable, go like this :locals => {:@instance_variable => value}
I am getting Missing template error, it seems like it doesn't found that partial and I have checked many times for spelling. still nothing.. Please help
You'll get "Missing template" error because ActionController::Base will search within action_controller/base path. Delegate to another controller. It usually inherits ActionController::Base anyway.
Also, if you want to use it with a regular page (not a partial), just leave out the :partial => part, like this: ac.render_to_string('/path/to/your/template', :locals => {:varable => somevarable}) (note the path is from inside the /views directory, e.g. you could have 'users/index.html.erb')
8

I had problems with a undefined helper method then I used ApplicationController

ApplicationController.new.render_to_string

2 Comments

Same here. Did you ever fix the undefined helper?
In my case i forgot a namespace when calling AplicationController. I try: Usr::ApplicationController.new.render_to_string And it works.
3

render_to_string is defined in ActionController::Base. Since the class/module is defined outside the scope of the Rails controllers the function is not available.

You are going to have to manually render the file. I don't know what you are using for your templates (ERB, Haml, etc.). But you are going to have load the template and parse it yourself.

So if ERB, something like this:

require 'erb'

x = 42
template = ERB.new <<-EOF
  The value of x is: <%= x %>
EOF
puts template.result(binding)

You will have to open the template file and send the contents to ERB.new, but that an exercise left for you. Here are the docs for ERB.

That's the general idea.

Comments

3

Rails 5

render_to_string and others are now available as class methods on the controller. So you may do the following with whatever controller you prefer: ApplicationController.render_to_string

I specifically needed to assign a dynamic instance variable for the templates based on an object's class so my example looked like:

ApplicationController.render_to_string(
  assigns: { :"#{lowercase_class}" => document_object },
  inline: '' # or whatever templates you want to use
)

Great blog post by the developer who made the rails PR: https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions

Comments

2

You could turn your template.xml.builder into a partial (_template.xml.builder) and then render it by instantiating an ActionView::Base and calling render

av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.extend ApplicationController.master_helper_module
xml = av.render :partial => 'something/template'

I haven't tried it with xml yet, but it works well with html partials.

1 Comment

I get: undefined method `new' for Rails::Configuration:Module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.