Ruby on rails ActiveAdmin注释作为提交系统
我有一个符合hippa的应用程序,我正在开发,作为其中的一部分,我想使用AA注释作为嵌套的资源/提交系统;如果没有注释,则应拒绝创建或更新。我有其他嵌套资源工作(包括嵌套在嵌套资源中保存);以下内容适用于更新,但不适用于新建-在新建时,我收到一个错误,comments.resource不能为空 我已经在许可参数中获得了注释属性;这里是admin/prescription.rb:Ruby on rails ActiveAdmin注释作为提交系统,ruby-on-rails,activeadmin,nested-attributes,Ruby On Rails,Activeadmin,Nested Attributes,我有一个符合hippa的应用程序,我正在开发,作为其中的一部分,我想使用AA注释作为嵌套的资源/提交系统;如果没有注释,则应拒绝创建或更新。我有其他嵌套资源工作(包括嵌套在嵌套资源中保存);以下内容适用于更新,但不适用于新建-在新建时,我收到一个错误,comments.resource不能为空 我已经在许可参数中获得了注释属性;这里是admin/prescription.rb: ActiveAdmin.register Prescription do menu :parent =>
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
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
在model/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
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
类处方true
有很多:注释,如::资源,依赖::销毁,类名:'ActiveAdmin::注释'
接受注释的\u嵌套\u属性\u
def至美国
“#{self.prescription_date.strftime(“%Y-%m-%d”)}:#{self.product}”
结束
结束
通过以上内容,我得到:
#<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"]}>
#,
@详细信息={:“comments.resource”=>[{:error=>:blank}]},
@messages={:“comments.resource”=>[“不能为空”]}>
我也在尝试手动操作(即@prescription=prescription.new(允许的参数[:prescription])并对building@comment执行相同的操作,但即使我正在设置
@comment.resource=@prescription--我仍然无法保存@prescription,因为comments.prescription为空;因为@prescription尚未保存
我确信我遗漏了一些可笑的东西,但不确定这可能是什么?对于那些关心此事的人,以下是我如何修复上述问题的;我成功地保存到了索引页(因为HIPA资源是空白的)而不是显示页面。我还没有实现用于删除的弹出式注释文本输入。我还为任何资源编写了以下通用内容——希望能够覆盖一些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