Ruby函数上下文与块

时间:2016-02-12 09:46:28

标签: ruby activeadmin

我试图计算以下内容: 与活动的admin ui builder一起使用。

为什么会起作用

def edit_action
    column "" do |resource|
        links = ''.html_safe
        links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource), :class => "member_link edit_link", :target => '_blank'
        links
    end
end
index do
    edit_action
end

虽然这不是

edit_action = Proc.new{
    column "" do |resource|
        links = ''.html_safe
        links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource), :class => "member_link edit_link", :target => '_blank'
        links
    end
}
index do
    edit_action.call
end

我得到的错误是对象模型(例如)没有方法'列'

1 个答案:

答案 0 :(得分:1)

我想说这是一个范围问题:def edit_action引入了一个新环境,而Proc.new从父级继承了环境。在第一种情况下,column被识别,因为ActiveAdminindex操作提供了column。在第二种情况下,定义Proc的范围对任何edit_action = Proc.new{...方法都不了解,因此您得到了您提到的错误。

如果您在index do...块中移动定义var Users = from usr in (from au in _lldat.aspnet_Users join am in _lldat.aspnet_Membership on au.UserId equals am.UserId select new { au.UserName, am.LoweredEmail }) group usr by new { usr.LoweredEmail } into grp select new { uEMail = grp.Key, uName = grp.Aggregate((a, b) => new {UserName = (a.UserName + ", " + b.UserName + ", "), LoweredEmail = a.LoweredEmail }).UserName }; ,它应该会再次运行。