0
votes

I have a rails form with nested views using fields_for:

new.html.erb

<%= form_for @leads do |f| %>
  <%= f.text_field :some_field %>
  <%= f.fields_for :tickets do |ticket_builder| %>
    <%= render 'tickets', f: ticket_builder %>
  <% end %>
<% end %>

_tickets.html.erb

f.text_field :some_other_field

There are situations where I need to add the tickets partial via ajax, and therefore I need to render that partial via a controller action:

  def add_ticket
    resp = {}

    resp[:html_options] = render_to_string(partial: "tickets", locals: { f: nil } )

    render text: resp.to_json
  end

Obviously this blows up since the f variable passed to the partial is nil, where it should be a form builder.

How can I pass a form builder to the partial from the controller?

1
as fact - you can't implement it without hooks after which next developers will curse you :) Depend's on what you want is better to user another solution. For example if you want to add tickets dynamically you may try this gem github.com/nathanvda/cocoon - D.K.

1 Answers

0
votes

Simply put into your controller:

include ActionView::Helpers::FormHelper