重要的是,当您更改链接时,即使使用 :dynamic_parameters 属性,除非您指定,否则它们不会重新呈现
config.list.refresh_with_header = true
在您的配置中。一旦你这样做了,它只是代码。
在我的例子中,我有一个带有可选附加文件的记录列表。我想在显示所有记录的列表和仅显示带有附件的记录之间切换。我有一个布尔参数:with_attachments,即'0' 或'1'。
动作链接看起来像
config.action_links.add :index, label: 'Show All', parameters: { with_attachments: '0' }, position: false
config.action_links.add :index, label: 'Attachment Only', parameters: { with_attachments: '1' }, position: false
config.list.refresh_with_header = true # Refresh action buttons!
index 方法将hidden 标签应用于当前选中的选项。
def index
current = params[:with_attachments] || '0'
active_scaffold_config.action_links.each do |link|
# For :index actions (filtering the list)
if link.action == :index
# hide the one that would refresh with the current parameter value
link.html_options[:hidden] = (link.parameters[:with_attachments] == current)
end
end
super
end
还有让隐藏动作不可见的 CSS
.active-scaffold-header .actions a[hidden=hidden] {
display: none;
}
您可能只需要一个按钮和 :dynamic_parameters 就可以做到这一点,但由于我通过 CSS 附加了图标(为简单起见省略了),因此该解决方案对我不起作用。