使用嵌套模型的2.3.x + rails功能,我认为我需要访问表单生成器实例才能正确地指定部分以呈现嵌套模型。从复杂形式示例中拉出来:
例如,这是一个封闭表单,用于创建表单构建器并将其传递给嵌套模型呈现视图:
<div class="children_fields tasks" id="<%= dom_id(f.object) %>_tasks"
data-context="<%= f.object_name %>">
<% f.fields_for :tasks do |task_form| %>
<%= render :partial => 'task', :locals => { :f => task_form } %>
<% end %>
</div>
任务部分是:
<%= f.label :name, "Task" %>
<%= f.text_field :name %>
<%= remove_child_link "remove", '#', f %>
<div class="children_fields assignments" id="<%= dom_id(f.object) %>_assignments"
data-context="<%= f.object_name %>">
<% f.fields_for :assignments do |assignment_form| %>
<%= render :partial => 'assignment', :locals => { :f => assignment_form } %>
<% end %>
</div>
当我尝试像这样指定一个部分时,我尝试通过执行以下操作来捕获表单构建器作为实例var:
before(:each) do
... # setup for target object
form_for [:foo, :bar, @project] do |f|
@f = f
end
end
这会对使用form_for
产生错误undefined method `polymorphic_path' for #<Spec::Rails::Example::ViewExampleGroup::Subclass_1:0x2cfeafc>
/Users/adamaig/.rvm/gems/ruby/1.8.7/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing'
/Users/adamaig/.rvm/gems/ruby/1.8.7/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!'
/Users/adamaig/.rvm/gems/ruby/1.8.7/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:272:in `form_for'
那么,正确的方法是: 1)获取这样的规格的FormBuilder实例? 2)spec嵌套模型及其视图形式?
这对于正确的名称生成很重要。
答案 0 :(得分:3)
解决方案是:
describe 'tasks/_form' do
attr_accessor :output_buffer
before do
task = mock_model(Task, :mumbo_jumbo => 'foo bar')
@output_buffer = ''
form_for(task, :url => task_path(task)) do |f|
@f = f
end
end
it 'should have input mumbo_jumbo' do
render_partial_with_locals
response.should have_tag('input[name=?]', 'task[mumbo_jumbo]')
end
private
def render_partial_with_locals
render :partial => 'tasks/form', :locals => {:f => @f}
end
def protect_against_forgery?
false
end
end