Passing ActionView::Helpers::FormBuilder to a partial

前端 未结 3 1114
渐次进展
渐次进展 2021-01-05 01:01

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

This is my setup:

View:

    <%= link_to \'Next\', check_         


        
相关标签:
3条回答
  • 2021-01-05 01:23

    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{})

    0 讨论(0)
  • 2021-01-05 01:28

    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, {})
    
    0 讨论(0)
  • 2021-01-05 01:40

    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.

    0 讨论(0)
提交回复
热议问题