以前我在主动管理资源中有表单和显示方法。那时表单中显示验证错误。但是现在我必须在添加更多验证错误之后在资源中编写控制器。难道我做错了什么。以下是我的代码。
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
答案 0 :(得分:2)
这是因为你刚刚在控制器中添加了空白的创建动作,
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
操作或写下它的实现。应该管用。