Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Button And Success Message In Active Admin Rails

I want to customize following things

  1. Actions Name like "Add User" => "Create User", "Edit User" => "Update User" etc
  2. Success Message On Delete, Create and Edit like "user successfully created" => "customer successfully created"
  3. Add A Create Button On Show Page beside edit and delete
like image 492
Mukesh Avatar asked Nov 26 '15 18:11

Mukesh


2 Answers

Yes, it is possible.

Actions Name like "Add User" => "Create User", "Edit User" => "Update User" etc

Instead of having f.actions, you could have

<%= f.actions do %>
  <%= f.action :submit, as: :button, label: 'Create User' %>
  <%= f.action :cancel, as: :link %> # change it to button if needed
<% end %>

ActiveAdmin uses formtastic, read more here.

Success Message On Delete, Create and Edit like "user successfully created" => "customer successfully created"

def create # or any other action
  super do |format| # this is important - override the original implementation
    redirect_to(
      admin_users_path,
      notice: 'Your custom message for successful user creation'
    ) and return
  end
end

You could also try this:

def create # or any other action
  super do |format| # this is important - override the original implementation
    flash[:notice] = 'Your custom message for successful user creation'
    # you do understand, that if you have different routes you should change this, right?
    redirect_to admin_users_path
  end
end

Add A Create Button On Show Page beside edit and delete

  action_item only: :show  do
    link_to 'Create new user', new_admin_users_path
  end
like image 195
Andrey Deineko Avatar answered Oct 08 '22 23:10

Andrey Deineko


I am adding answer for second(refrence from above), But On validation errors above is not working so i customize it which can help you better

   controller do

    def update 
      super do |format| 
        if !@your_object.errors.any?
          redirect_to(
            admin_my_localities_path,
            notice: 'custom message.'
          ) and return
        end
      end
    end


    def destroy 
      super do |format| 
        if !@your_object.errors.any?
          redirect_to(
            admin_my_localities_path,
            notice: 'custom message.'
          ) and return
        else
          redirect_to(
            admin_my_localities_path,
            alert: 'custom error.'
          ) and return
        end
      end
    end
end
like image 25
Mukesh Avatar answered Oct 09 '22 01:10

Mukesh