8

I am atempting to dinamically create form elements given a certain AJAX request.

This is my setup:

View:

    <%= link_to 'Next', check_unique_id_students_path, :remote => true %>

    <div id="guardian_student_details"></div>

Controller:

def check_unique_id
    @student = Student.new
    @this_form = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
  end

JS:

jQuery("#guardian_student_details").html("<%=escape_javascript(render :partial => "student_details", :locals => { :s => @this_form }) %>");

Partial:

<% puts s.text_field :first_name   %>
<% puts s.field_helpers   %>

For debugging purposes i placed the following lines at the very beginning of my partial:

<% puts s.class.to_s %>
<% puts s.object.to_s %>

This prints out :

ActionView::Helpers::FormBuilder
Student

This should work. However rails is giving the following error:

ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
1: <% puts s.class.to_s   %>
2: <p>
3: <%= s.text_field :first_name, :class => 'text_input is_empty' %>
4: <%= s.label :first_name %><strong>*</strong> 
5: </p>
6: 

app/views/students/_student_details.html.erb:3:in _app_views_students__student_details_html_erb__2485891544130782916_2214680440' app/views/students/check_unique_id.js.erb:2:in_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'

Which implies that "s" is NIL something I verified just 2 lines before. Does anybody have any ideas? i dont know if this has something to do with the "@template" variable initialized in the controller. Which i played around with and accepts practically anything and if printed is nil. Any help would be appreciated. Thanks

Final note:

I tried to implement this: AJAX update of accepts_nested_attributes_for partials

Community
  • 1
  • 1
jalagrange
  • 2,291
  • 2
  • 19
  • 24

3 Answers3

6

For anyone needing to build a form builder in the controller, view_context still works there. Using Rails 4.1.4:

@object = Object.new
@f = ActionView::Helpers::FormBuilder.new(:object, @object, view_context, {})
d3vkit
  • 1,942
  • 1
  • 24
  • 36
4

In the view, I've found that 'view_context' does not work in Rails 3.1. Instead try 'self' when creating a FormBuilder object.

s = ActionView::Helpers::FormBuilder.new(:student, @student, self, {}, proc{})

sciguy
  • 41
  • 1
0

Try this in a console :

s = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
s.text_field :first_name

You will have the same error. I think the problem come from your creation of the form_builder object, even if I don't know the exact mistake...

Your solution seems to me to be a little much complex. You can try this solution :

#html.erb
<% form_for @student do |f| %>
  <div id='guardian_student_details' class='hide-this-block'>
    <%= render :partial => "student_details", :locals => { :s => f }) %>
  </div>
<% end %>

#js
jQuery("#guardian_student_details").show();

Generally, I prefer keep javascript and ruby separated.

Alexandre Butynski
  • 6,625
  • 2
  • 31
  • 44
  • Thanks Alexandre, I know this works, but my functionality would greatly benefit if I could do it in the proposed way with the FormBuilder. – jalagrange Sep 02 '11 at 01:46
  • Is this in Rails 2.3 or 3+ ? Not sure if the @template variable exists in Rails 3. With the AJAX update post you mentioned I've changed the code since I posted that method. In the end I used a modified version of http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf instead of grabbing the existing index and incrementing it (it was prone to breaking especially if I wanted to extend the nesting to another level). – Pasted Sep 05 '11 at 10:27
  • 1
    Hello Pasted, this is rails 3.1. Thank you for your comments and links. I have tried implementing your solution using view_context instead of @template and it breaks in the following way: NameError: undefined local variable or method `view_context' for main:Object – jalagrange Sep 08 '11 at 19:33