Hopstream
Hopstream

Reputation: 6451

ActiveAdmin -- how to reference current object?

How can you reference the instance of the object you are currently viewing?

The following WORKS

ActiveAdmin.register Example do

  sidebar "test" do
    @name = example.name
  end

end

The following DOESN'T Work

ActiveAdmin.register Example do

  member_action :some_stuff, :method => :put do
    @name = example.name
  end

end

How can I reference the object in the member_action?

Or will I have to create another instance?

Upvotes: 3

Views: 5813

Answers (2)

Vlad Hilko
Vlad Hilko

Reputation: 1244

You can use 'resource' object.

ActiveAdmin.register Example do

  member_action :some_stuff, :method => :put do
    @name = resource.name
  end

end

Upvotes: 5

Brett Bender
Brett Bender

Reputation: 19749

Most of the active admin documentation is out of date or completely nonexistent. You will likely have to read the source and hope somebody commented functions if you want the nitty-gritty on how to use it.

The member_action function documentation is as follows:

# Member Actions give you the functionality of defining both the
# action and the route directly from your ActiveAdmin registration
# block.
#
# For example:
#
#   ActiveAdmin.register Post do
#     member_action :comments do
#       @post = Post.find(params[:id]
#       @comments = @post.comments
#     end
#   end
#
# Will create a new controller action comments and will hook it up to
# the named route (comments_admin_post_path) /admin/posts/:id/comments
#
# You can treat everything within the block as a standard Rails controller
# action.
# 

This makes it look like they expect you to perform your own object lookup in custom actions - Post.find(params[:id]).

Upvotes: 4

Related Questions