Activeadmin: 如何传递验证错误并保留值

创建于 2016-12-08  ·  3评论  ·  资料来源: activeadmin/activeadmin

你好,我有一个自定义控制器(我将它用作问题 #4685 的补丁)
我需要解决的问题是验证错误和保留表单数据。 在我做自定义控制器之前,错误会像这样在输入字段下方弹出

screen shot 2016-12-07 at 5 43 52 pm

但现在他们没有出现。 请帮忙! 谢谢 :)

ActiveAdmin.register

controller do
        def create
          <strong i="12">@section</strong> = AbqCouncilors::Councilor.create!(permitted_params[:abq_councilors_councilor].as_json)
              redirect_to admin_faq_sections_path, notice: "Section was successfully created!"          
        end
    end

最有用的评论

我通过以下更改得到了它。
重要必须有@resource作为实例变量。 不会与任何其他人一起工作

controller do
        def create
          <strong i="9">@resource</strong> = AbqCouncilors::Councilor.new(permitted_params[:abq_councilors_councilor].as_json)
            if @resource.save
                flash[:notice] = "Councilor was successfully created!"
                redirect_to admin_abq_councilor_path(@resource.id)
        else
            flash[:error] = "Your form is missing or has incomplete fields. Please review your entry below."
                render action: 'new'
         end
        end
    end

所有3条评论

看起来它应该在验证器错误时引发错误或继续在列表上重定向。 您必须在create操作方法中编写额外的逻辑。

@dmitry有例子吗?

我通过以下更改得到了它。
重要必须有@resource作为实例变量。 不会与任何其他人一起工作

controller do
        def create
          <strong i="9">@resource</strong> = AbqCouncilors::Councilor.new(permitted_params[:abq_councilors_councilor].as_json)
            if @resource.save
                flash[:notice] = "Councilor was successfully created!"
                redirect_to admin_abq_councilor_path(@resource.id)
        else
            flash[:error] = "Your form is missing or has incomplete fields. Please review your entry below."
                render action: 'new'
         end
        end
    end
此页面是否有帮助?
0 / 5 - 0 等级

相关问题

thejspr picture thejspr  ·  3评论

seanlinsley picture seanlinsley  ·  3评论

Nitrino picture Nitrino  ·  3评论

daya picture daya  ·  3评论

zhdwwf picture zhdwwf  ·  4评论