【发布时间】:2018-08-13 18:07:18
【问题描述】:
我将 ActiveAdmin 添加到我的应用程序并成功更改了资源的索引方法。现在,当我单击“新资源”时,它会将我带到新方法,但是,缺少一个按钮(回形针)以允许用户上传图像附件。
我找不到编辑视图的方法,也找不到完全重写新方法的方法。
如果你需要我的任何代码,我可以在这里粘贴所有内容。
谢谢! // 查看这篇文章的最底部的解决方案!
//这是我尝试过的方法,但它不起作用。我为索引方法应用到“app/admin/entry.rb”的更改有效,但“新”方法根本不起作用。
app/admin/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 添加了一个脚手架,并像这样使用它: entry_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 已经以某种方式使用的现有代码?我可以通过简单地添加我需要的一个字段来将其更改为我的需要。
// 解决方案:
app/admin/resource.rb
ActiveAdmin.register Entry do
permit_params :image, :description
index do
column :id
column :description
column :created_at
column :image_file_name
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
form do |f|
f.inputs "New Entry" do
f.input :description
f.input :image
end
f.actions
end
end
【问题讨论】: