docs/8-custom-actions.md in activeadmin-0.6.6 vs docs/8-custom-actions.md in activeadmin-1.0.0.pre1
- old
+ new
@@ -10,130 +10,151 @@
resources. This method adds both the action to the controller as well as
generating a route for you.
To add a collection action, use the collection_action method:
+```ruby
+ActiveAdmin.register Post do
- ActiveAdmin.register Post do
+ collection_action :import_csv, method: :post do
+ # Do some CSV importing work here...
+ redirect_to collection_path, notice: "CSV imported successfully!"
+ end
- collection_action :import_csv, :method => :post do
- # Do some CSV importing work here...
- redirect_to {:action => :index}, {:notice => "CSV imported successfully!"}
- end
+end
+```
- end
+This collection action will generate a route at `/admin/posts/import_csv`
+pointing to the `Admin::PostsController#import_csv` controller action.
-This collection action will generate a route at "/admin/posts/import_csv"
-pointing to the Admin::PostsController#import_csv controller action.
-
## Member Actions
A member action is a controller action which operates on a single resource.
For example, to add a lock action to a user resource, you would do the
following:
- ActiveAdmin.register User do
+```ruby
+ActiveAdmin.register User do
- member_action :lock, :method => :put do
- user = User.find(params[:id])
- user.lock!
- redirect_to {:action => :show}, {:notice => "Locked!"}
- end
+ member_action :lock, method: :put do
+ resource.lock!
+ redirect_to resource_path, notice: "Locked!"
+ end
- end
+end
+```
-This will generate a route at "/admin/users/:id/lock" pointing to the
-Admin::UserController#lock controller action.
+This will generate a route at `/admin/users/:id/lock` pointing to the
+`Admin::UserController#lock` controller action.
-## Controller Action HTTP Verb
+## HTTP Verbs
-The collection_action and member_actions methods both accept the "method"
+The `collection_action` and `member_action` methods both accept the `:method`
argument to set the HTTP verb for the controller action and route.
-The generated routes will be scoped to the given method you pass in. By default
-your action will use the :get verb.
+Sometimes you want to create an action with the same name, that handles multiple
+HTTP verbs. In that case, this is the suggested approach:
-## Rendering in Custom Actions
+```ruby
+member_action :foo, method: [:get, :post] do
+ if request.post?
+ resource.update_attributes! foo: params[:foo] || {}
+ head :ok
+ else
+ render :foo
+ end
+end
+```
+## Rendering
+
Custom controller actions support rendering within the standard Active Admin
layout.
- ActiveAdmin.register Post do
+```ruby
+ActiveAdmin.register Post do
- # /admin/posts/:id/comments
- member_action :comments do
- @post = Post.find(params[:id])
+ # /admin/posts/:id/comments
+ member_action :comments do
+ @comments = resource.comments
+ # This will render app/views/admin/posts/comments.html.erb
+ end
- # This will render app/views/admin/posts/comments.html.erb
- end
+end
+```
- end
-
If you would like to use the same view syntax as the rest of Active Admin, you
can use the Arbre file extension: .arb.
-For example, create app/views/admin/posts/comments.html.arb with:
+For example, create `app/views/admin/posts/comments.html.arb` with:
- table_for assigns[:post].comments do
- column :id
- column :author
- column :body do |comment|
- simple_format comment.body
- end
- end
+```ruby
+table_for assigns[:post].comments do
+ column :id
+ column :author
+ column :body do |comment|
+ simple_format comment.body
+ end
+end
+```
-### Custom Action Items
+## Page Titles
-To include your own action items (like the New, Edit and Delete buttons), add an
-`action_item` block. For example, to add a "View on site" button to view a blog
-post:
+The page title for the custom action will be the translated version of
+the controller action name. For example, a member_action named "upload_csv" will
+look up a translation key of `active_admin.upload_csv`. If none are found, it
+defaults to the name of the controller action.
- action_item :only => :show do
- link_to('View on site', post_path(post)) if post.published?
- end
+If this doesn't work for you, you can always set the `@page_title` instance
+variable in your controller action to customize the page title.
-Actions items also accept the :if option to conditionally display them:
+```ruby
+ActiveAdmin.register Post do
- action_item :only => :show, :if => proc{ current_admin_user.super_admin? } do
- "Only display this to super admins on the show screen"
- end
+ member_action :comments do
+ @comments = resource.comments
+ @page_title = "#{resource.title}: Comments" # Sets the page title
+ end
-### Page Titles
+end
+```
-The page title for the custom action will be the internationalized version of
-the controller action name. For example, a member_action named "upload_csv" will
-look up a translation key of "active_admin.upload_csv". If none are found, it
-just title cases the controller action's name.
+# Action Items
-If this method doesn't work for your requirements, you can always set the
-@page_title instance variable in your controller action to customize the page
-title.
+To include your own action items (like the New, Edit and Delete buttons), add an
+`action_item` block. The first parameter is just a name to identify the action,
+and is required. For example, to add a "View on site" button to view a blog
+post:
- ActiveAdmin.register Post do
+```ruby
+action_item :view, only: :show do
+ link_to 'View on site', post_path(post) if post.published?
+end
+```
- # /admin/posts/:id/comments
- member_action :comments do
- @post = Post.find(params[:id])
- @page_title = "#{@post.title}: Comments" # Set the page title
+Actions items also accept the `:if` option to conditionally display them:
- # This will render app/views/admin/posts/comments.html.erb
- end
+```ruby
+action_item :super_action, only: :show, if: proc{ current_admin_user.super_admin? } do
+ "Only display this to super admins on the show screen"
+end
+```
- end
+# Modifying the Controller
-## Modify the Controller
-
The generated controller is available to you within the registration block by
-using the #controller method.
+using the `controller` method.
- ActiveAdmin.register Post do
+```ruby
+ActiveAdmin.register Post do
- controller do
- # This code is evaluated within the controller class
+ controller do
+ # This code is evaluated within the controller class
- def define_a_method
- # Instance method
- end
- end
-
+ def define_a_method
+ # Instance method
end
+ end
+
+end
+```