How to visualize RAILS with local variable f in new.js.erb?

There is a way to display the erb page in js.erb as follows: remote => true in rails:

$('#invoice_against_lease').html('$("<%= j render(:file => 'invoice/new.html.erb') %>")');

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 %>  

Partially displayed in html.erb like this, with a local variable constructor passed in:

<%= f.simple_fields_for :invoice_items do |builder| %>
  <%= render 'customer_quote_record', :f => builder %>
<% end %>

Tried the code below:

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

And the error "ActionView::Template::Error (undefined local variable or methodf '... "`

Is there a way to do partial above in js.erb?

+5
source share
3 answers

Try the following:

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

This, of course, suggests that it fis defined wherever you make this call. If it is different, just change :locals => {:f => f}to:locals => {:f => "YOUR_VARIALBE"}

+10
source

Another way to do this:

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

+2
source

, :

in js.rjs file, I return form_for and fields_for helper and save the fields_for construct in the instance variable @builderand then pass it to the partial ( 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}))%>");
+1
source

All Articles