如何使用ActiveAdmin Gem以编辑形式插入显示代码

时间:2014-05-24 09:19:33

标签: ruby-on-rails activeadmin

我有一个问题查看页面,用户可以在其中查看所有问题。另外,我有一个“创建新问题”的链接,点击后会允许用户添加新问题。

此代码如下:

ActiveAdmin.register QuestionType do

menu :if => proc{ !current_admin_user.present? }

permit_params :name

index do 
selectable_column
column :name
actions
end

show do 
 attributes_table :name
end

form do |f|
 f.inputs "Add Question type" do
   f.input :name
 end
 f.actions
end

end

我的代码工作正常,但我想在添加新问题时显示所有问题。我想在“添加新问题”按钮的提交下面显示所有问题,以便用户也可以看到现有问题。我怎样才能做到这一点。

1 个答案:

答案 0 :(得分:2)

app / views /

文件夹下创建 admin_partials 文件夹

admin_partials 文件夹中创建文件 _question_form.html.erb ,以便目录结构如 app / views / admin_partials / _question_form.html.erb

将以下代码放在该文件中: -

<%= form_for @question_type, :url => admin_question_types_path , :html => { :class => 'form-horizontal' } do |f| %>
 <fieldset class="inputs">
  <ol>
   <% unless @question_type.nil? %>
    <%= hidden_field_tag :id, @question_type.id %>
   <% end %>
   <%  f.object.errors.full_messages.each do |y| %>
    <%= y %> <br />
   <% end %>

   <li class="string input required stringish">
     <%= f.label :name, :class => 'label'%>
     <div class="controls">
       <%= f.text_field :name, :class => 'text_field' %>
     </div>
   </li>

   <li class="string input required stringish">
     <%= f.submit nil, :class => 'btn btn-primary' %>
       <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            admin_question_types_path, :class => 'btn' %>
   </li>

  </ol>
 </fieldset>
<% end %>

<h3>All Question types</h3>
<ol>
 <% QuestionType.all.each do|question_type| %>
  <li><span><%= question_type.name %></span><span><%= link_to "Show", question_type  %></span><span><%= link_to "Delete", question_type, method: :delete, data: { confirm: 'Are you sure?' }  %></span></li>
 <% end %>
</ol>

将此文件编辑为: -

ActiveAdmin.register QuestionType do

menu :if => proc{ !current_admin_user.present? }

permit_params :name

index do 
selectable_column
column :name
actions
end

show do 
 attributes_table :name
end

form :partial => "admin_partials/question_form"

end