After adding controller in active admin resource , validations error cannot be shown

本小妞迷上赌 提交于 2019-12-12 05:08:14

问题


Previously i have form and show method in active admin resource. At that time validation errors shown in forms. But now i have to write controller in resource , after adding no more validation errors. Am i doing something wrong. Below is my code.

ActiveAdmin.register PsychographicsQuestion do

  config.clear_action_items!
  permit_params :id , :title,:psychographics_question_type ,:expiry_date , :_destroy , psychographics_options_attributes: [:id,:title , :_destroy]

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Question" do   
      f.input :title , label: "Question"
      f.input :psychographics_question_type, label: "Question Type" ,:prompt => 'Select Answer Type', :as => :select, :collection => DemographicsQuestion.demographics_question_types.keys.to_a
      f.input :expiry_date, as: :datepicker, datepicker_options: { min_date: 0}
    end
    f.has_many :psychographics_options,heading: 'Options' do |item|
      item.input :title 
    end
    f.actions
  end

  show do
    attributes_table do
      row :title
      row :psychographics_question_type
      row :expiry_date
    end
  end
  controller do
    def create
      question = PsychographicsQuestion.create(permitted_params["psychographics_question"])

      if question.id != nil

        PsychographicsQuestionFilter.create(@@filters)
        filter_user = User.search(@@users)

        for u in filter_user.result
          FiltersMatching.create(:user_id => u.id, :psychographics_filters_id => @@filters.to_i , :psychographics_question_id => question.id)
            #send_msg_through_gcm(u.to_i,"New PsychoGraphics question has been added.")
            NotificationsDatum.create(:user_id => u.id , :msg => "New PsychoGraphics question has been added." , :category => NotificationsDatum.categories["PsychographicsQuestion"] , :status=>NotificationsDatum.statuses["Active"] , :viewed_status=>NotificationsDatum.viewed_statuses["NotViewed"] , :psychographics_question_id => question.id)
          end

          redirect_to admin_psychographics_question_path(:id => 93)
        else
          redirect_to new_admin_psychographics_question_path(:id => question.id)
        end
      end
    end

    filter :expiry_date
    filter :created_at
  end 

回答1:


This is because you just added blank create action in controller,

controller do
  def create
     create! { |success, failure|
     success.html do
       redirect_to admin_customers_path, :notice => "Resource created successfully."
     end
     failure.html do
       flash[:error] = "Error(s) : #{resource.errors.full_messages.join(',')}"
       redirect_to :back 
     end
   }
 end
end

Just remove blank create action or write it's implementation. Should work.



来源:https://stackoverflow.com/questions/37965870/after-adding-controller-in-active-admin-resource-validations-error-cannot-be-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!