我将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添加了一个脚手架,并按如下方式使用它:entrys_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已经以某种方式使用的现有代码?我可以通过简单地添加我需要的一个字段来将其更改为我的需要。
// 解决方案:
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
您可以自定义控制器操作和新资源视图。
要在控制器中编辑新动作,请执行以下操作:
#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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句