Rails ActiveAdmin -编辑新的资源视图



我将ActiveAdmin添加到我的应用程序中,并成功地更改了我的资源的索引方法。现在,当我点击"新资源"时,它会把我带到新方法,但是,为了允许用户上传图像附件,缺少一个按钮(回形针)。

我找不到编辑视图的方法,也找不到完全重写new-method的方法。

如果你需要我的任何代码,我可以粘贴在这里。

谢谢!//检查这篇文章的最底部的解决方案!

//这就是我尝试的方式,但是它不起作用。我对"app/admin/entry"进行了修改。对于我的index-method, Rb '有效,但是'new'-method根本不起作用。

应用程序/管理/entry.rb:

ActiveAdmin.register Entry do
  index do
    column :id
    column :description
    column :created_at
    column :image_content_type
    column do |entry|
      links = link_to "Edit", edit_admin_entry_path(entry)
      links += " "
      links += link_to "Delete", admin_entry_path(entry), :method => :delete, data: { confirm: "Are you sure?" }
      links
    end
  end
  def new
    form_for @entry, :html => {:multipart => true} do |f|
      f.label :description
      f.text_area :description
      f.file_field :image
    end
    f.submit 'Save'
  end
end

在我添加ActiveAdmin之前,我只是为Entry添加了一个scaffold,并像这样使用它:entries_controller.rb:

  def new
    @entry = Entry.new
  end

视图(new.html.slim):

h1 New entry
== render 'form'
= link_to 'Back', entries_path

呈现的表单(_form.html.slim):

= form_for @entry, :html => {:multipart => true} do |f|
  - if @entry.errors.any?
    #error_explanation
      h2 = "#{pluralize(@entry.errors.count, "error")} prohibited this entry from being saved:"
      ul
        - @entry.errors.full_messages.each do |message|
          li = message
  .field
    = f.label :description
    = f.text_area :description
    = f.file_field :image
  .actions = f.submit 'Save'

现在,虽然这在标题为localhost:3000/entries/new时仍然有效,但它只是显示了localhost:3000/admin/entries/new

的默认视图

如果你得到任何帮助,我将不胜感激!是否有任何方法可以查看ActiveAdmin已经使用的现有代码?我可以根据我的需要修改它,只需添加一个我需要的字段。

//解决方案:

<标题>应用程序/管理/resource.rb h1> div class="one_answers">

您可以自定义控制器动作和新的资源视图。

编辑控制器中的新动作:

#app/admin/your_resource.rb
controller do
  def new
    @resource = Resource.new
    .... # Your custom logic goes here
  end
end

编辑新的资源视图并添加带有回形针的图像。

#app/admin/your_resource.rb
form html: { multipart: true } do |f|
  f.inputs "Resource Details" do
    f.input :title
    .... # Your input fields
    # This adds the image field. Be careful though 
    # the field name needs to be the same in your model
    f.input :image, as: :file, required: false
  end
  f.actions
end

最新更新