最近开发应用的时候,希望将某些页面的内容做为消息发送给第三方的程序,因此想要在 rails 中单独启动一个线程做这件事,从网上找到的方法大概如下:
class TaskActionView < ActionView::Base
include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers
include ApplicationHelper
def default_url_options
{host: 'example.com'}
end
end
def action_view
controller = ActionController::Base.new
controller.request = ActionDispatch::TestRequest.new
TaskActionView.new(Rails.root.join('app', 'views'), {}, controller)
end
tv = action_view()
tv.assign(:group =>Group.new)
tv.render(:template=>"groups/_form")
在实际的调用中,一直遇到这样的错误:
ActionView::Template::Error:
undefined method `model_name' for NilClass:Class
# ./app/views/groups/_form.html.erb:1:in `_app_views_groups__form_html_erb___2686766126405061367_2175427740'
# ./app/models/web_agent.rb:18:in `render'
# ./spec/models/web_agent_spec.rb:19:in `block (2 levels) in <top (required)>'
其中 groups/_form.html.erb 文件中包含有
<%= form_for @group,:html => {:id=>"group_form",:class=>"form-vertical"},:remote=>true do |f| %>
这个问题谁有经验,能分享一下吗?