ActiveAdmin - 如何引用当前对象?

时间:2011-11-11 14:15:09

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 activeadmin

如何引用当前正在查看的对象的实例?

以下工作

ActiveAdmin.register Example do

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

end

以下不起作用

ActiveAdmin.register Example do

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

end

如何引用member_action中的对象?

或者我必须创建另一个实例吗?

2 个答案:

答案 0 :(得分:4)

大多数有效的管理员文档已过期或完全不存在。您可能需要阅读源代码并希望有人评论函数,如果您想要了解如何使用它的细节。

member_action function documentation如下:

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

这使他们看起来希望您在自定义操作中执行自己的对象查找 - Post.find(params[:id])

答案 1 :(得分:1)

您可以使用'资源'对象

ActiveAdmin.register Example do

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

end