render_to_string 与 url 参数
render_to_string with url params
我有一个 sidekiq worker,它应该渲染一个文件并保存它。
在通过 render_to_string
的渲染过程中,我传递了一堆实例变量和像 current_user
这样的变量,效果很好。
但要呈现我的视图,我还需要 url 个参数,例如 params[:action]
。如何使用 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 %>
我得到的错误:
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
无效:
:params => [action: 'conclusion']
:@params => [action: 'conclusion']
我也遇到了所有路径的问题:
<%= url_for :host =>'http://localhost:3000/', only_path: true, controller: API::V1::CommentsController, action: :load_more_comments %>
...
..
.
抛出
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']
是一个内部有散列的列表,要使 params[:action] 起作用,您必须执行 :params => { action: 'myaction' }
更新1:
一个名为 params
的变量似乎在 rails 中得到了特殊处理。您可以执行以下操作
# 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 %>
所以避免使用参数,而是将值作为局部变量传递给模板。
我有一个 sidekiq worker,它应该渲染一个文件并保存它。
在通过 render_to_string
的渲染过程中,我传递了一堆实例变量和像 current_user
这样的变量,效果很好。
但要呈现我的视图,我还需要 url 个参数,例如 params[:action]
。如何使用 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 %>
我得到的错误:
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
无效:
:params => [action: 'conclusion']
:@params => [action: 'conclusion']
我也遇到了所有路径的问题:
<%= url_for :host =>'http://localhost:3000/', only_path: true, controller: API::V1::CommentsController, action: :load_more_comments %>
...
..
.
抛出
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']
是一个内部有散列的列表,要使 params[:action] 起作用,您必须执行 :params => { action: 'myaction' }
更新1:
一个名为 params
的变量似乎在 rails 中得到了特殊处理。您可以执行以下操作
# 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 %>
所以避免使用参数,而是将值作为局部变量传递给模板。