[英]Rails ActiveAdmin - Edit the new resource-view
我將ActiveAdmin添加到應用程序中,並成功更改了資源的索引方法。 現在,當我單擊“新資源”時,將我帶到新方法就可以了,但是,缺少一個按鈕(回形針),以便允許用戶上傳圖像附件。
我找不到編輯視圖的方法,也找不到完全重寫新方法的方法。
如果您需要我的任何代碼,我可以將所有內容粘貼在這里。
謝謝! //請在這篇文章的最底部查找解決方案!
//這是我嘗試過的方法,但是沒有用。 我對索引方法應用於“ app / admin / entry.rb”的更改有效,但是“新”方法根本不起作用。
應用程序/管理/ 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.