3

如何引用當前正在查看的對象的實例?ActiveAdmin - 如何引用當前對象?

WORKS

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?或者我會不得不創建另一個實例?

回答

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

您可以使用'資源'對象。

ActiveAdmin.register Example do 

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

end