undefined method `has_many' for Formtastic

你。 提交于 2019-12-21 12:59:48

问题


I'm getting this error :

undefined method `has_many' for #<Formtastic::SemanticFormBuilder:0xb410d4c>

It work when I use it like this :

ActiveAdmin.register Ressource do
    form do |f|  
        f.inputs do
            f.input :offer_id, :as => :hidden
            f.input :name
            f.input :category, :include_blank => false, :collection => Category.order('place ASC').all, :member_label => :to_label
            f.input :description, :input_html => {:class => 'editor'}
            f.input :price
        end
        f.has_many :roles do |app_f|
            app_f.inputs do
                if not app_f.object.id.nil?
                    app_f.input :_destroy, :as => :boolean, :label => "Supprimer l'utilisateur du lot"
                end
                app_f.input :user, :member_label => :to_label, :label => 'Assigné le lot'
                app_f.input :name, :include_blank => false
            end
        end
        f.buttons
    end
end 

But it doesn't work in a partial, i need to call the has_many method by a different way or something else ?


回答1:


ActiveAdmin extends formtastic with some useful helpers such as has_many (lib/active_admin/form_builder.rb in the activeadmin gem).

Unfortunately, these helpers are not available by default in your templates.

Here are two options:

  1. If you don't need the extra has_many functionality (it looks like active_admin adds some javascript to make it easy to add a new record to the collection), then you can use stock formtastic. This example should work fine in the activeadmin file as well as in a partial:

    ActiveAdmin.register Ressource do
      form do |f|  
        # ...
        f.inputs :for => :roles do |app_f|
          # ...
          app_f.input :name, :include_blank => false
        end
        f.buttons
      end
    end 
    
  2. Use the ActiveAdmin form builder explicitly:

    <%= semantic_form_for [:admin, @resource], builder: ActiveAdmin::FormBuilder do |f| %>
      <!-- ... -->
    
      <%= f.has_many :teachers do |app_f| %>
        <%= app_f.inputs do %>
          <!-- ... -->
        <% end %>
      <% end %>
    
      <%= f.buttons %>
    <% end %>
    

I hope this helps.




回答2:


There is a solution

form :html => {:multipart => true} do |f|
end

Or, if you want to use partial:

<%= active_admin_form_for [:admin, @resource] ,:html => {:multipart => true} do |f|%>
<% end %>


来源:https://stackoverflow.com/questions/10070779/undefined-method-has-many-for-formtastic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!