我对此有一个问题:我有一个名为的部分_return_item.html.erb
,所以我想反复动态地渲染它。
<%= f.fields_for :return_items, return_item do |item_form| %>
some_field_input_for_return_item
<%end%>
index.html.erb
是这样的:
<%= form_for(@return_request) do |f| %>
<div id="contact">
<!-- some_field_input_for_request_contact_info -->
</div>
<div id="itemlist">
<!-- This part is working-->
<% @return_request.return_items.each do |item| %>
<%= render :partial => "return_items/return_items", :locals => {:f => f, :return_item => item}%>
<!-- I_want_to_append_the_partial_here -->
<%end%>
</div>
<%= button_tag("Add New Item", :class=>"addItem" )%>
<%end%>
<script>
$(document).ready( function () {
$( '.addItem' ).click(function () {
$.ajax({
url: "return_requests/request_new_item",
data: {
item_num: ($( '#itemlist' ).children().length)
},
dataType: "script"
})
return false;
});
});
</script>
request_new_item
控制器是这样的:
def request_new_item
@return_item = ReturnItem.new
end
这是request_new_item.js.erb
:
$("#itemlist").append('<%= j render :partial => "return_items/return_items", :locals => {:f => f, :return_item => item } %>');
当我按下按钮addItem
时,我希望生成一个新的部分,但实际上,这不起作用。控制台提供错误:
ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0x007f5a302232b0>:0x007f5a380afea8>)
更新:我忘了发布模型
return_request.rb
::
class ReturnRequest < ActiveRecord::Base
has_many :return_items, :class_name=>'ReturnItem', :foreign_key=>'RMA_Nmb', :primary_key=>'RMA_Nmb'
accepts_nested_attributes_for :return_items
end
return_item.rb
:
class ReturnItem < ActiveRecord::Base
belongs_to :return_request, :class_name=>"ReturnRequest"
end
那么,任何人都可以解释一下这是怎么发生的,我该如何解决这个问题?谢谢