Activeadmin: How to pass validation errors and keep values

Created on 8 Dec 2016  路  3Comments  路  Source: activeadmin/activeadmin

Hello, I have a custom controller (I'm using it as a patch for issue #4685)
the problem I need to fix is the validation errors and keeping the form data. Before I did the custom controller, the errors would pop up underneath the input field like so

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

but now they do not show up. Please help! Thanks :)

ActiveAdmin.register

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

Most helpful comment

I got it with the below change.
Important Must have @resource as the instance variable. Would not work with any other

controller do
        def create
          @resource = 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

All 3 comments

Looks like it should raise an error on validator error or proceed to redirect on a list. You have to write additional logic in create action method.

@dmitry have an example?

I got it with the below change.
Important Must have @resource as the instance variable. Would not work with any other

controller do
        def create
          @resource = 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
Was this page helpful?
0 / 5 - 0 ratings