有一种方法可以在js.erb中使用以下方式呈现erb页面:remote =>在rails中为true:
$('#invoice_against_lease').html('$("<%= j render(:file => 'invoice/new.html.erb') %>")');
我们有一个像这样的部分_customer_quote_record:
<%= f.input :quote_id, :label => 'Quote#:', :collection => quotes_for_invoice(@customer), :include_blank => true %>
<%= f.hidden_field :_destroy %>
部分以html.erb的形式呈现,并传递局部变量builder:
<%= f.simple_fields_for :invoice_items do |builder| %>
<%= render 'customer_quote_record', :f => builder %>
<% end %>
尝试下面的代码:
$('#invoice_against_lease').html('$("<%= j render(:file => 'customer_lease_record', :f => f) %>")');
错误是"ActionView::Template::Error (undefined local variable or method
f'...“`
有没有办法在js.erb中渲染上面的部分?
答案 0 :(得分:13)
尝试以下方法:
$('#invoice_against_lease').html('$("<%= j render(:partial => 'customer_lease_record', :locals => {:f => f}) %>")');
当然,这假定在您拨打此电话的任何地方都定义了f
。如果不同,只需将:locals => {:f => f}
更改为:locals => {:f => "YOUR_VARIALBE"}
答案 1 :(得分:2)
看看这个,我找到了一个解决方案:
在js.rjs文件中,我重新生成form_for和fields_for帮助器,并将fields_for构造函数保存在实例变量@builder
中,然后将其传递给部分({{1}})
js.rjs:
locals: {f: @builder...
答案 2 :(得分:2)
另一种方法:
<%=j render "invoice/new", f: f %>