我有一个符合hippa标准的应用程序,我正在努力,作为其中的一部分,我想使用AA注释作为嵌套资源/提交系统;如果没有评论,则应拒绝创建或更新。我有其他嵌套资源工作(包括嵌套在嵌套资源保存);以下是关于更新但不适用于新的 - 关于新的我收到错误 comments.resource不能为空。
我在许可证参数范围内得到了comments_attributes;这是admin / prescription.rb:
ActiveAdmin.register Prescription do
menu :parent => "Treatments", :priority => 2
permit_params :treatment_id, :hours, :summary, :product_id, :prescription_date, prescribed_tensions_attributes: [:id, :location, :force, :position, :prescription_id, :created_at, :_destroy], comments_attributes: [:id, :namespace, :body, :resource_id, :resource_type, :author_id, :author_type]
before_filter :only => [:show, :destroy, :edit, :update] do
# @prescription = Prescription.unscoped.includes(:prescribed_tensions).find(params[:id])
@prescription = Prescription.unscoped.includes(:product, :prescribed_tensions, :patient, :doctors).find(params[:id])
end
form do |f|
f.inputs "Prescribed Dose" do
f.input :treatment, :as => :select, input_html: {class: 'select2able'}
f.input :prescription_date, as: :date_time_picker
f.input :hours
f.input :summary, :as => :rich, :config => { :width => '79%', :height => '150px' }
f.has_many :prescribed_tensions, :allow_destroy => true do |g|
g.input :location, :as => :select, input_html: {class: 'select2able'}, collection: BaseProduct.locations
g.input :force
g.input :position
end
end
f.inputs "Add A Comment" do
f.semantic_fields_for :comments, ActiveAdmin::Comment.new do |c|
c.inputs :class => "" do
c.input :body, :label => "Comment", :input_html => { :rows => 4 }
end
end
end
f.actions
end
controller do
def setup_comments
klassname = self.resource_class.name.underscore
if params[klassname][:comments_attributes]['0']['body'].blank?
err = "A comment must be added to #{params[:action]} this #{klassname}."
else
params[klassname][:comments_attributes]['0']['namespace'] = 'admin'
params[klassname][:comments_attributes]['0']['author_id'] = current_admin_user.id
params[klassname][:comments_attributes]['0']['author_type'] = 'AdminUser'
end
if !err.nil?
params[:error] = err
end
return
end
def update(options={}, &block)
setup_comments
# save resource
if params[:error].nil?
super
if resource.errors.any?
params[:error] = resource.errors.full_messages.first
end
end
# see if any error messages
if !params[:error].nil?
redirect_to({ action: 'edit' }, alert: params[:error])
end
end
def create(options={}, &block)
setup_comments
if params[:error].nil?
super
if resource.errors.any?
params[:error] = resource.errors.full_messages.first
end
end
if !params[:error].nil?
redirect_to({ action: 'index' }, alert: params[:error])
end
end
end
end
在模型/处方中.rb:
class Prescription < ActiveRecord::Base
belongs_to :treatment
has_one :product, through: :treatment
has_one :patient, through: :product
has_many :doctors, through: :patient
has_many :prescribed_tensions, dependent: :destroy
accepts_nested_attributes_for :prescribed_tensions, :allow_destroy => true
has_many :comments, as: :resource, dependent: :destroy, class_name: 'ActiveAdmin::Comment'
accepts_nested_attributes_for :comments
def to_s
"#{self.prescription_date.strftime('%Y-%m-%d')} : #{self.product}"
end
end
通过以上我得到:
#<Prescription:0x007fb402700f90
id: nil,
hours: 12,
summary: "",
prescription_date: Mon, 15 May 2017 09:31:00 EDT -04:00,
created_at: nil,
updated_at: nil,
treatment_id: 6>,
@details={:"comments.resource"=>[{:error=>:blank}]},
@messages={:"comments.resource"=>["can't be blank"]}>
我也试图手工完成这项工作(即 @ prescription = Prescription.new(allowed_params [:处方]),同样建立@comment,但即使我'设置
@ comment.resource = @prescription - 我仍然无法保存@prescription,因为comments.prescription为空;由于@prescription尚未保存。
我确定我在这里遗漏了一些荒谬的东西,但不确定那可能是什么......?
答案 0 :(得分:2)
对于那些关心的人,以下是我如何解决上述问题;我成功保存到索引页面(对于hippa资源是空白的)而不是显示页面。我还没有为删除实现弹出注释文本输入。我还将以下内容写成任何资源的通用 - 很愿意能够覆盖一些AA资源(不是全部)并在共享代码中实现它,但也无法解决这个问题。
controller do
# hippa compliant blank
def apply_filtering(chain)
if params['q'].blank?
@search = chain.ransack({})
chain.none
else
super
end
end
def setup_comments
klassname = self.resource_class.name.underscore
if params[klassname][:comments_attributes]['0']['body'].blank?
err = "A comment must be added to #{params[:action]} this #{klassname}."
else
params[klassname][:comments_attributes]['0']['namespace'] = 'admin'
params[klassname][:comments_attributes]['0']['author_id'] = current_admin_user.id
params[klassname][:comments_attributes]['0']['author_type'] = 'AdminUser'
end
if !err.nil?
params[:error] = err
end
return
end
def update(options={}, &block)
setup_comments
# save resource
if params[:error].nil?
super
if resource.errors.any?
params[:error] = resource.errors.full_messages.first
end
end
# see if any error messages
if !params[:error].nil?
redirect_to({ action: 'edit' }, alert: params[:error])
end
end
def create
setup_comments
if params[:error].nil?
resource = self.resource_class.new(permitted_params[self.resource_class.name.underscore.to_sym])
@comment=ActiveAdmin::Comment.new(permitted_params[self.resource_class.name.underscore.to_sym][:comments_attributes]['0'])
@comment.resource = resource
resource.comments.first.resource = resource
if resource.valid?
resource.save
else
if resource.errors.any?
params[:error] = resource.errors.full_messages.first
end
end
end
if !params[:error].nil?
redirect_to({ action: 'index' }, alert: params[:error])
else
redirect_to({ action: 'index' }, alert: "#{resource_class} was successfully saved with comment.")
end
end
end