[英]How to add an action in active_admin in rails
我正在 Rails 中的 active_admin 上工作,我想在其中添加一个将运行服务的操作。
动作应该出现在索引视图中。 那么,我如何添加运行此服务的操作。
我的 app/admin/website.rb 代码是
ActiveAdmin.register Website do
actions :index
index do
selectable_column
id_column
column :state
column :name
column :created_at
column :updated_at
end
end
在上面的文件中,我想检查 id state='draft'
然后它应该运行运行服务的操作。
我的服务文件 app/services/website_verify_state_service.rb:
class WebsiteVerifyStateService
def self.process!(website)
new(website).process
end
def initialize(website)
@website = website
end
def process
site_response = self.class.post("#{BASE_MONO_URL}reseller/account/site",
site_data = JSON.parse(site_response.body)['data']
if site_data
@website.update(published: true) if site_data[0]['site']['lastPublishDate'].present?
end
end
end
所以我唯一担心的是我如何通过活动管理员的操作调用此服务。 提前致谢
您可以向index
表添加条件操作:
ActiveAdmin.register Website do
actions :index
index do
selectable_column
id_column
column :state
column :name
column :created_at
column :updated_at
actions do |website|
item('Verify state', verify_admin_websites_path(website.id), class: 'member_link') if website.state == 'draft'
end
end
member_action :verify do
# do your magic here, like or whatever you do
WebsiteVerifyStateService.process!
redirect_back(fallback_location: root_path)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.