如何在 new.js.erb 中使用局部变量 f 渲染 RAILS 部分? [英] How to render a RAILS partial with local variable f in new.js.erb?
问题描述
有一种方法可以像这样在 js.erb 中渲染 erb 页面,在 rails 中使用 :remote => true :
There is a way to render a erb page in js.erb like this with :remote => true in rails:
$('#invoice_against_lease').html('$("<%= j render(:file => 'invoice/new.html.erb') %>")');
我们有一个像这样的部分_customer_quote_record:
We have a partial _customer_quote_record like this:
<%= f.input :quote_id, :label => 'Quote#:', :collection => quotes_for_invoice(@customer), :include_blank => true %>
<%= f.hidden_field :_destroy %>
部分在 html.erb 中呈现为这样,并传递局部变量构建器:
The partial is rendered in html.erb as this, with passing local variable 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中渲染上面的部分?
Is there a way to render the partial above in js.erb?
推荐答案
尝试以下操作:
$('#invoice_against_lease').html('$("<%= j render(:partial => 'customer_lease_record', :locals => {:f => f}) %>")');
这当然假设 f
在您进行此调用的任何地方都已定义.如果不同,只需更改 :locals =>{:f =>f}
到 :locals =>{:f =>"YOUR_VARIALBE"}
This of course assumes that f
is defined wherever you make this call. If it's different, just change :locals => {:f => f}
to :locals => {:f => "YOUR_VARIALBE"}
这篇关于如何在 new.js.erb 中使用局部变量 f 渲染 RAILS 部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!