开发者

ActiveAdmin - how to render default template in customized action

开发者 https://www.devze.com 2023-03-19 23:46 出处:网络
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 fie

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 mi开发者_开发技巧ssing 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.


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"


The solution mentioned at this other stackoverflow post worked:

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


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).


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消