Ruby on rails Activeadmin自定义操作和表单
我想在我的用户activeadmin页面上实现一个自定义操作(notify_all),单击该页面时将显示一个表单,提交该表单时将路由到另一个自定义操作(send_notification_to_all)。到目前为止,我无法让第二部分工作 admin/users.rb:Ruby on rails Activeadmin自定义操作和表单,ruby-on-rails,activeadmin,Ruby On Rails,Activeadmin,我想在我的用户activeadmin页面上实现一个自定义操作(notify_all),单击该页面时将显示一个表单,提交该表单时将路由到另一个自定义操作(send_notification_to_all)。到目前为止,我无法让第二部分工作 admin/users.rb: ActiveAdmin.register User do action_item :only => :index do link_to 'Notify All', notify_all_admin_users_
ActiveAdmin.register User do
action_item :only => :index do
link_to 'Notify All', notify_all_admin_users_path
end
collection_action :notify_all, :method => :get do
puts "notifying...."
end
collection_action :send_notification_to_all, :method => :post do
puts "sending notification...."
end
end
单击“全部通知”按钮时,将呈现以下视图。
views/admin/users/notify_all.html.erb
<form action="send_notification_to_all" method="post">
<div><textarea rows="10" cols="100" placeholder="Enter message here"></textarea></div>
<div><input type="submit"></div>
</form>
是否可以通过active admin执行我试图执行的操作?在一个类似的问题中找到了答案 我修改了我的表单以包含身份验证令牌,如下所示:
<form action="send_notification_to_all" method="post">
<input type="hidden" name="authenticity_token" value="#{form_authenticity_token.to_s}">
<div><textarea rows="10" cols="100" placeholder="Enter message here"></textarea></div>
<div><input type="submit"></div>
</form>
这解决了这个问题。使用Rails、Formtastic或ActiveAdmin表单生成器可以完全避免这个问题,因为它会自动为您呈现真实性令牌 使用Formtastic的
semantic\u form\u for
form builder重写表单:
<%= semantic_form_for :notification, url: { action: :send_notification } do |f| %>
<%= f.inputs do %>
<%= f.input :content, as: :text, input_html: { placeholder: "Enter message here" } %>
<%- end %>
<%= f.actions %>
<%- end %>
可能值得通过Formtastic阅读更多细节。默认情况下,ActiveAdmin中包含Formtastic。使用Arbre可以编写
form do |f|
input type: :hidden, name: 'authenticity_token', value: form_authenticity_token.to_s
我没有通知模型,是否可以在没有模型的情况下使用formtastic?是的,上面使用的符号
:notification
,只是表单发送的参数的名称空间。它不需要数据模型。在您的操作中,您可以通过params[:notification][:content]
访问通知内容。
form do |f|
input type: :hidden, name: 'authenticity_token', value: form_authenticity_token.to_s