Jhon
Jhon

Reputation: 111

create in rails 4 active admin

I have a user model and inside active admin i have written like

ActiveAdmin.register User do
  controller do

    def permitted_params
       params.require(:user).permit(:username,:email,:password,:password_confirmation,
                           :admin, :locked, :first_name, :last_name, :work_phone, :cell_phone,
                           :cell_carrier, :fax, :temp_password,
                           :active, :company_id, :group_id, role_ids:[])
    end

    def create
      @user = User.new(params[:user])
      if @user.save
        redirect_to admin_users_path
      else
        render :new
      end
    end
  end
end

But when ever i am trying to create the user its showing an error like. ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError: What i am doing wrong ?

Upvotes: 1

Views: 782

Answers (1)

Josh Kovach
Josh Kovach

Reputation: 7749

Inherited Resources is a little weird with permitted params. You don't actually get to require key. You have to pass a hash to the permit method.

If you're using the latest version of ActiveAdmin, you should also be able to use the permit_params method.

ActiveAdmin.register User do
  permit_params :username, :email, :password, :password_confirmation,
                :admin, :locked, :first_name, :last_name, :work_phone, :cell_phone,
                :cell_carrier, :fax, :temp_password,
                :active, :company_id, :group_id, role_ids:[]
  end
end

Also, if you are going to override the create method, you must use permitted_params in place of params[:user], which is most likely the cause of the current error you're getting. It doesn't look like you're actually doing anything special in your custom create action, though, so unless you plan to do something more, you should probably just let ActiveAdmin handle the controller actions.

Upvotes: 1

Related Questions