2017-05-15 4 views
1

私はヒッパに準拠したアプリを手に入れました。その一部として、ネストされたリソース/コミットシステムとしてAAコメントを使用したいと思います。コメントがない場合は、作成または更新を拒否する必要があります。他のネストされたリソースが動作しています(ネストしたリソースをネストして保存しています)。次の更新は動作しますが、新機能ではなく新機能ではありません。comments.resourceは空白にできないというエラーが表示されますコミットシステムとしてのActiveAdminのコメント

私は、許可パラメータ内にcomments_attributesを持っています。ここでは、管理/ 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 

とモデル/ prescription.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(permitted_pa​​rams [:prescription])と@commentをビルドして同じことをやっていますが、私は設定していますが

@ comment.resource = @prescription - comments.prescriptionは空白なので、まだ@prescriptionを保存することはできません。 @prescriptionはまだ保存されていません。

私はここに何かばかげていることが間違っていると確信していますが、それがどういうものかは不明です.... ...?

答えて

2

気になる人のために、以下は私が上記をどのように修正したかです。私は、ショーページではなく、インデックスページ(hippaリソースが空白の場合)に成功しました。私はまだ、削除のためにポップアップコメントのテキスト入力を実装することはまだありません。私はまた、以下のようなリソースをすべてのリソースに対して汎用的に書いています。AAリソースの一部をオーバーライドして共有コードで実装することができるLOVEですが、それを理解することはできませんでした。

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 
関連する問題