ActiveAdmin - how to display default template in custom action

We use ActiveAdmin in our Rails3 application for default models. Now we had to rewrite the action of the show. The OrderProcess model is a transitional (without tables) model, which means that all fields are aggregated from other data. We use an internal module that provides the necessary methods for bullying MetaSearch methods. ActiveAdmin depends on. The following describes 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 

This gives us an error complaining about the missing template "The template admin / order_processes / show with ... is missing"

We also tried to call

  render renderer_for(:show) 

but this caused an error regarding the missing method model_name, which may be due to the fact that our model does not have tables and the corresponding module.

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

+6
source share
6 answers

Just stumbled upon this ... The grant comment is correct, active_admin_template no longer exists (I'm on 1.0.0-pre2).

I ended up with:

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

which seems to work, although you will need to provide a shortcut for the title, which appears as "no translation: en.active_admin. [your_action] _model"

+3
source

The solution mentioned in this other postoverflow post worked:

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

I had a similar problem when I needed to override the default behavior of the active controller administrator for the update action. I got it to work as follows:

 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 simply render :edit , which displays the default editing page already defined by the active administrator.

Another solution using

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

didn't work for me or for any other render renderer_for(:edit) combination.

+2
source

I have the same problem: (

I am trying to override the update action and trying to execute 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 
+1
source

Activeadmin docs are very easy on how to override the action of a standard controller, which is disappointing given how opaque the source code is. Many of the internal elements in the stone seem to have changed a ton with version 1.0, which renders many old stack overflow responses unusable.

In any case, here is 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 is worth noting that activeadmin expects, if your model is a user, for the create action to have a populated instance of the model as @user before it can display action => :new .

I wrote the insides of my custom creation method as a class method on my model so that I can test it and bury as little code as possible in my activeadmin code.

For context, I needed to override this action because I use Devise, and I wanted my admins to create user accounts with a temporary password and a custom greeting email, not built-in: a confirmed email for self-creation of an account.

Here is this 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
source

This answer requires a β€œ2019 update” for ActiveAdmin 1.4.

In my example, if you want to customize a new action and visualize something other than just a form, this is what you do.

  controller do def new @page_title = 'New <your resource name>' render :new, layout: 'active_admin' end end 

Without @page_title title on your page will look like "New" or "Edit," rather than the usual "". Also, without specifying layout: 'active_admin' , you will get an empty layout.

0
source

Source: https://habr.com/ru/post/892508/


All Articles