8

有一种方法可以在 js.erb 中像这样在 rails 中使用 :remote => true 来呈现 erb 页面:

$('#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 中呈现,并传递局部变量构建器:

<%= 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 methodf'..."`

有没有办法在 js.erb 中渲染上面的部分?

4

3 回答 3

15

尝试以下操作:

$('#invoice_against_lease').html('$("<%= j render(:partial => 'customer_lease_record', :locals => {:f => f}) %>")');

这当然假设f在您进行此调用的任何地方都定义了它。如果不同,只需更改:locals => {:f => f}:locals => {:f => "YOUR_VARIALBE"}

于 2012-07-27T18:38:26.620 回答
5

另一种方法:

<%=j render "invoice/new", f: f %>

于 2016-08-20T23:39:58.537 回答
2

看看这个,我找到了解决办法:

在 js.rjs 文件中,我重新生成了 form_for 和 fields_for 帮助器,并将 fields_for 构造函数保存在实例变量@builder中,然后将其传递给部分(locals: {f: @builder...

js.rjs:

<%
  @expense=Expense.new
  new_expense_detail=@expense.expense_details.build
  form_for(@expense) do |f| 
    f.fields_for(:expense_details,new_expense_detail,:child_index=>@child_index) do |builder| 
      @builder=builder # <<--- New line compared js.rjs
    end
  end
%>

$("#cost_center_group_<%=@child_index%>").html("<%= escape_javascript(render(partial: 'select_costcenter', locals: {f: @builder,child_index: @child_index}))%>");
于 2014-03-10T12:47:14.863 回答