I have a sidekiq worker, which should render a file and save it.
In the rendering process via render_to_string
i pass a bunch of instance variables and variables like current_user
, which works fine.
But to render my view I also need url parameters like params[:action]
. How can I pass them to the view by using render_to_string
?
# SpecialWorker.rb
class ConclusionRenderingWorker < AbstractController::Base
include Sidekiq::Worker
ac = ActionController::Base.new()
data = ac.render_to_string(:template => "my/rendering_view.html.erb",
layout: false,
:locals => {
:@instance_variable1 => instance_variable1,
:params => [action: 'myaction']
}
)
# my/rendering_view.html.erb
<% unless params[:action] == 'dashboard' %>
Blah
<% else %>
Blubb
<% end %>
The error I got:
2015-08-07T13:20:20.103Z 54344 TID-oxuugdhn4 WARN: "class"=>"ConclusionRenderingWorker", "args"=>[1, 18], "retry"=>true, "queue"=>"default", "jid"=>"1c97d91dc5db162ecd225418", enqueued_at"=>1438953617.792468, "error_message"=>"undefined method `parameters' for nil:NilClass", error_class"=>"ActionView::Template::Error", failed_at"=>1438953620.102505, "retry_count"=>0}
2015-08-07T13:20:20.103Z 54344 TID-oxuugdhn4 WARN: undefined method `parameters' for nil:NilClass
Does not work:
:params => [action: 'conclusion']
:@params => [action: 'conclusion']
I also have the problem with all paths:
<%= url_for :host =>'http://localhost:3000/', only_path: true, controller: API::V1::CommentsController, action: :load_more_comments %>
...
..
.
which throws
undefined method `host' for nil:NilClass
/Users/jan/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/action_controller/metal/url_for.rb:30:in `url_options'
/Users/jan/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/action_view/helpers/url_helper.rb:37:in `url_options'
/Users/jan/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
/Users/jan/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/action_view/helpers/url_helper.rb:107:in `url_for'
:params => [action: 'myaction']
is a list with a hash inside, for params[:action] to work, you have to do :params => { action: 'myaction' }
Update1:
A variable called params
seems to get special handling inside rails. You could do the following
# SpecialWorker.rb
class ConclusionRenderingWorker < AbstractController::Base
include Sidekiq::Worker
ac = ActionController::Base.new()
data = ac.render_to_string(:template => "my/rendering_view.html.erb",
layout: false,
:locals => {
:@instance_variable1 => instance_variable1,
:action => 'myaction'
}
)
# my/rendering_view.html.erb
<% unless action == 'dashboard' %>
Blah
<% else %>
Blubb
<% end %>
So avoid using params, but just pass on the values as local variables to the template.