未定义的方法'has_many'for Formtastic [英] undefined method `has_many' for Formtastic
问题描述
我遇到此错误:
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
但是它不能部分运行,我需要通过其他方式或其他方式调用has_many方法?
But it doesn't work in a partial, i need to call the has_many method by a different way or something else ?
推荐答案
ActiveAdmin通过一些有用的帮助程序(例如 has_many
( lib / active_admin / form_builder.rb
(在activeadmin gem中)。
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.
有两个选项:
-
如果不需要额外的
has_many
功能(好像active_admin添加了一些javascript以便于向收藏夹添加新记录变得容易),那么您就可以使用股票格式了。此示例在activeadmin文件以及部分代码中应该都能正常工作。
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
显式使用ActiveAdmin表单构建器:
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 %>
我希望这会有所帮助。
这篇关于未定义的方法'has_many'for Formtastic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!