You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Active Admin中Lightbox组件data-lightbox属性无内容仅显示href的问题

Hey there! Let's troubleshoot why your data-lightbox attribute isn't showing up in your Active Admin photo links. Here are a few targeted fixes and checks to resolve this:

1. Switch to Modern Ruby Hash Syntax

While both old (:data => {...}) and new (data: {...}) hash syntax work in Rails, Active Admin's context can sometimes have subtle preferences for the modern style. Update your code to use this cleaner syntax:

index do
  selectable_column
  id_column
  column :validate_by_us
  column :resource
  column "Photo" do |photo|
    link_to image_tag(photo.photo_url), image_path(photo.photo_url), data: { lightbox: photo.photo_url }
  end
  actions
end

2. Ensure photo.photo_url Has a Valid Value

If photo.photo_url returns nil or an empty string, Rails will automatically skip rendering the data-lightbox attribute entirely. Add a fallback value to guarantee the attribute appears:

column "Photo" do |photo|
  # Use the photo's URL or a unique fallback ID to ensure the attribute renders
  lightbox_id = photo.photo_url.presence || "photo-#{photo.id}"
  link_to image_tag(photo.photo_url), image_path(photo.photo_url), data: { lightbox: lightbox_id }
end

3. Check for Custom Active Admin Overrides

If you’ve modified Active Admin’s helper methods (like overriding link_to in initializers or custom helpers), this could interfere with attribute rendering. Double-check any custom code that might alter link behavior.

4. Verify Rendered HTML Directly

Open your browser’s developer tools (right-click → Inspect) on the photo link to confirm:

  • The href attribute is correctly populated
  • There are no typos in the data-lightbox attribute name (Rails automatically converts the :lightbox symbol to data-lightbox, so this should be correct)

After applying these changes, your data-lightbox attribute should appear in the rendered link tag, letting Lightbox recognize and use it properly.

内容的提问来源于stack exchange,提问作者H.Akca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:03:25