ActiveAdmin - how to render default template in customized action
Asked Answered
R

5

6

We are using ActiveAdmin in our Rails3 application for the default models. Now we needed to overwrite the show action. The OrderProcess model is a transient (tableless) model, which means that all fields are aggregated from other data. We use an internal module that provides the necessary methods to mock the MetaSearch methods ActiveAdmin is depending on. The following is how we overwrite the show action:

ActiveAdmin.register OrderProcess do  
  member_action :show, :method => :get do
    @order_process = OrderProcess.all_orders_for_deal(params['id'])
  end
end

That gives us an error complaining about a missing template "Missing template admin/order_processes/show with ..."

We also tried to call

  render renderer_for(:show)

but that produced an error about a missing method model_name which may be due to our model being tableless and the regarding module.

How can we use ActiveAdmins built in rendering methods to display our model? Any help is appreciated.

Ratliff answered 11/7, 2011 at 16:6 Comment(0)
B
3

Just ran into this... Grant's comment is correct, active_admin_template doesn't exist any more (I'm on 1.0.0-pre2).

I ended up going with:

render :action => :edit, :layout => false

which seems to work, although you will have to supply a label for the header, which displays as "translation missing: en.active_admin.[your_action]_model"

Bathsheeb answered 16/4, 2016 at 13:57 Comment(2)
to supply label for the header use @page_title = "Lable".Retainer
This doesn't work anymore (1.2.1). The view renders without any layout at all.Bathsheeb
H
2

I have the same problem :(

I'm trying to override an update action and trying to render the 'edit action'

member_action :update, :method => :post do
  if params[:user][:password].blank?
    [:password, :password_confirmation, :current_password].collect{|p| params[:user].delete(p) }
  end

  @user = User.find(params[:id])
  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to([:admin, @user]) }
    else
      format.html { render renderer_for(:edit) }
    end
  end
end
Hurtle answered 25/7, 2011 at 2:36 Comment(1)
Why are you creating member_action for update? update is included in standard curd methods so try to override it in controller e.g controller do def update # your changes end endKeratin
F
2

The solution mentioned at this other stackoverflow post worked:

render active_admin_template('edit.html.arb'), :layout => false
Flamethrower answered 6/4, 2012 at 7:22 Comment(2)
Might have changed in a recent version. I'm getting undefined method 'active_admin_template'Eberhart
How about: render 'admin/*resource_name*/*view_name*', :layout => falseBilateral
M
2

I had a similar issue where I needed to override the default active admin controller behavior for the update action. I got it to work like this:

controller do
  def update
    @model = Model.find(params[:id])
    # do stuff
    if @model.save
      redirect_to admin_model_path(@model)
    else
      render :edit
    end
  end
end

The key was just render :edit which will render the default edit page already defined by active admin.

The other solution using

render active_admin_template('edit.html.arb'), :layout => false

did not work for me or any other combination of render renderer_for(:edit).

Misdoubt answered 16/4, 2014 at 19:9 Comment(0)
C
1

The activeadmin docs are very light on the specifics of how to override a standard controller action, which is frustrating given how opaque the source code is. Many of the internals in the gem seem to have changed a ton with version 1.0, which renders a lot of older Stack Overflow answers unusable.

Anyway, here's how I was above to override the #create action in my activeadmin controller (on Rails 4.2.x):

  controller do
    def create
      @user = User.create_from_admin(permitted_params[:user])

      if @user.persisted?
        redirect_to resource_path(@user), notice: I18n.t("admin.user.create.notice")
      else
        render :action => :new
      end
    end
  end

It's worth noting that activeadmin expects, if your model is User, for the create action to have a populated model instance as @user before it can render action => :new.

I wrote the internals of my custom create method as a class method on my model so I could unit-test it and bury as little code as possible in my activeadmin code.

For context, I needed to override this action because I'm using Devise and I wanted to let my admins create user accounts with a temporary password and a custom welcome email rather than the built-in :confirmable email for self-created accounts.

Here's that User class method:

  def self.create_from_admin(params)
    generated_password = Devise.friendly_token.first(8)

    @user                    = User.new(params)
    @user.password           = generated_password
    @user.skip_confirmation!

    if @user.save
      # Code to send custom email with temp password
    end

    @user
  end
Chapen answered 21/12, 2017 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.