ruby-on-railsrubyrails-admin

Rails admin custom controller and form


I am using rails_admin gem to create an admin dashboard and I am trying to create a custom controller to create new admins, but just with one input: email.

I am using devise gem.

The problem is that when I try to use rails_admin_form_for, I get this error:

ActionView::Template::Error (undefined method `admin_registration_path' for #<ActionView::Base:0x0000000000e470>

My html.erb is:

<%= rails_admin_form_for @object, url: admin_registration_path(model_name: @abstract_model.to_param) do |f| %>
  <%= f.label :email %>
  <%= f.email_field :email %>

  <%= f.submit "Sign up" %>
<% end %>

My controller is:

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdmin
  module Config
    module Actions
      class NewAdmin < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :collection do
          true
        end

        # register instance custom name
        register_instance_option :link_icon do
          'fas fa-user-plus'
        end

        register_instance_option :http_methods do
          %i[get post]
        end

        register_instance_option :controller do
          proc do
            if request.get?
              @object = @abstract_model.new
              @action = @action.with(@action.bindings.merge(object: @object))
              @authorization_adapter&.attributes_for(:new, @abstract_model)&.each do |name, value|
                @object.send("#{name}=", value)
              end
              object_params = params[@abstract_model.param_key]
              if object_params
                sanitize_params_for!(request.xhr? ? :modal : :create)
                @object.assign_attributes(@object.attributes.merge(object_params.to_h))
              end
              respond_to do |format|
                format.html { render @action.template_name }
              end
            elsif request.post?
              puts "Action: #{@action}"
            end
          end
        end
      end
    end
  end
end

What I am doing wrong or there are better ways to do that?

I followed this code

Edit: These are the routes: enter image description here


Solution

  • You have to fully qualify your routes calls like this:

    Rails.application.routes.url_helpers.admin_registration_path