Rails AJAX: My partial needs a FormBuilder instance

后端 未结 5 642
梦如初夏
梦如初夏 2020-12-09 04:52

So I\'ve got a form in my Rails app which uses a custom FormBuilder to give me some custom field tags

<% form_for :staff_member, @staff_member, :builder =         


        
相关标签:
5条回答
  • 2020-12-09 04:59

    how about this?

      @template.with_output_buffer do
        @template.form_for @model_object do |f|
          f.fields_for :some_nested_attributes do |ff|
            render :partial => 'nested_attributes', :object => @model_object, :locals => {:form => ff}
          end
        end
      end
    

    this would be especially useful is you need to use the nested fields_for in the partial

    0 讨论(0)
  • 2020-12-09 04:59

    You could instantiate a new instance of your form builder in the controller, though it feels sort of lousy to me:

    # in the controller
    render :partial => {
      :f => MyFormBuilder.new(:staff_member, @staff_member, template),
      :skill_groups => @skill_groups,
      :staff_member => @staff_member
    }
    

    Alternatively, you could move more of the update logic to be client side which wouldn't require you to worry about rendering anything at all. You could just update the values via JS. Not sure if that works for your project though.

    0 讨论(0)
  • 2020-12-09 05:18

    Use fields_for inside your partial. It performs a similar task but without wrapping the form tags. See the API docs.

    0 讨论(0)
  • 2020-12-09 05:19

    Maybe I'm a little late in the game here, and maybe I don't understand the question properly, but in ApplicationHelper.rb I think you can just add the line:

    ActionView::Base.default_form_builder = MyFormBuilder
    
    0 讨论(0)
  • 2020-12-09 05:21

    You can submit within your ajax call the content of f.object_name (it's also works with partials) and use it to render tags defined in http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html passing it as the first argument.

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