Active Admin中Lightbox组件data-lightbox属性无内容仅显示href的问题
data-lightbox Attribute in Active Admin Link 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
hrefattribute is correctly populated - There are no typos in the
data-lightboxattribute name (Rails automatically converts the:lightboxsymbol todata-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

