在活动管理资源中添加控制器后,无法显示验证错误
After adding controller in active admin resource , validations error cannot be shown
以前我在活动管理资源中有表单和显示方法。当时验证错误显示在表格中。但是现在我必须在不添加更多验证错误之后在资源中编写控制器。难道我做错了什么。下面是我的代码。
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
这是因为您刚刚在控制器中添加了空白创建操作,
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
只需删除空白 create
操作或编写其实现。应该管用。
以前我在活动管理资源中有表单和显示方法。当时验证错误显示在表格中。但是现在我必须在不添加更多验证错误之后在资源中编写控制器。难道我做错了什么。下面是我的代码。
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
这是因为您刚刚在控制器中添加了空白创建操作,
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
只需删除空白 create
操作或编写其实现。应该管用。