Ruby-on-rails Active Admin:仅自定义新表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6725233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Active Admin: Customize only new form
提问by Michael Irwin
I'm using Active Admin to provide an admin to some models. I need to provide a customized new form for one of them, but leave the edit form as the default provided by Active Admin. Here's what I have. It works in that it is giving me the new form I want, but the edit form is also using the new form, which is not what I want:
我正在使用 Active Admin 为某些模型提供管理员。我需要为其中一个提供自定义的新表单,但将编辑表单保留为 Active Admin 提供的默认表单。这就是我所拥有的。它的工作原理是它给了我我想要的新表单,但编辑表单也在使用新表单,这不是我想要的:
ActiveAdmin.register Document do
form :partial => 'form'
end
I've tried this, but it gives an error that 'new' is an undefined method:
我试过这个,但它给出了一个错误,即“新”是一个未定义的方法:
ActiveAdmin.register Document do
new do
form :partial => 'form'
end
end
采纳答案by Michael Irwin
I've figured out a way to do it with some logic in the view. Not the best way, to be sure, but it does what I want until I figure out a better way. Here's the logic I'm using:
我已经想出了一种方法来在视图中使用一些逻辑来做到这一点。当然,这不是最好的方法,但在我找到更好的方法之前,它会做我想做的事。这是我正在使用的逻辑:
<% if controller.action_name == 'new' %>
new form
<% else %>
edit form
<% end -%>
回答by ryanb006
If you just want to hide or show certain fields on the new form (e.g. a field that you generate automatically in the model using before_create), you can do this:
如果您只想隐藏或显示新表单上的某些字段(例如,您使用 before_create 在模型中自动生成的字段),您可以这样做:
form do |f|
f.inputs "Member Details" do
f.input :first_name
f.input :last_name
f.input :email
if !f.object.new_record?
f.input :password
f.input :password_confirmation
end
end
f.button :Submit
end
This will hide the password fields when you create a new member in the event that you automatically generate passwords the first time the member is created.
如果您在第一次创建成员时自动生成密码,这将在您创建新成员时隐藏密码字段。
回答by Serabe
I am not sure it can be done directly with form. If you take a look at the codeyou'll see that only the last call is taken into account. On the other hand, you may try something like:
我不确定它可以直接用form. 如果您查看代码,您会发现只考虑了最后一次调用。另一方面,您可以尝试以下操作:
config.set_page_config :new do
form :partial => 'form'
end
But I would rather ask the developers for this feature.
但我更愿意向开发人员询问此功能。
回答by across
If someone wants to render different partials for new and edit pages you have to:
如果有人想为新页面和编辑页面呈现不同的部分,您必须:
#app/admin/document.rb
ActiveAdmin.register Document do
form partial: 'form'
end
#app/views/admin/documents/_form.html.erb
<% if @document.new_record? %>
<%= render partial: "form_new", resource: @document %>
<% else %>
<%= render partial: "form_edit", resource: @document %>
<% end %>
#app/views/admin/documents/_form_new.html.erb
<%= semantic_form_for [:admin, @document], builder: Formtastic::FormBuilder do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :name %>
<% end %>
<%= f.actions %>
<% end %>
回答by mattfitzgerald
You could create a custom page that acts as the new form, render a partial there which contains arbitrary form code.
您可以创建一个用作新表单的自定义页面,在那里呈现包含任意表单代码的部分。
So, in your admin directory you make a file new_document.rbcontaining
因此,在您的 admin 目录中,您创建了一个new_document.rb包含
ActiveAdmin.register_page "New Document" do
content do
panel "Create a new document" do
render :partial => "admin/documents/custom_form", :locals => {document: Document.new}
end
end
end
You then put your arbitrary formtastic form in admin/documents/custom_formand your arbitrary controller action aka collection_actionin admin/documents.
然后你把你的任意formtastic形式admin/documents/custom_form和任意波形控制器动作又名collection_action中admin/documents。
So basically doing normal rails type stuff within the activeadmin framework.
所以基本上在 activeadmin 框架内做正常的 rails 类型的东西。

