7

I have this code:

div class: item.ui_type do
  link_to image_tag(item.image.image_public_url), item.target)
  link_to item.label, item.target
end

Basically, I want a div with 2 links inside. However, only the last element is getting rendered, presumably because what gets rendered inside the body is the return value of the block.

I know I can declare those as an array and join them, but then I need to call html_safe. I'm trying to find a way to do this when you actually don't trust the input that you're receiving.

This seems like it should be an extremely simple thing to do, but I can't find it anywhere.

Any pointers?

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
  • This may help http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag – Sonalkumar sute Mar 03 '15 at 10:21
  • It seems that the next code is working well: `Arbre::Context.new { div class: 'test' do a 'first link', href: 'first_link'; a 'second_link', href: 'second_link' end }` – atomdev Mar 03 '15 at 10:32

2 Answers2

1

I suspect the reason atomdev's example works and Daniel's does not is that link_to is a Rails tag helper and a is an Arbre tag, see this similar issue. Try wrapping link_to with text_node, div or span. It might be worth opening an Arbre GitHub issue for this.

Piers C
  • 2,880
  • 1
  • 23
  • 29
1

Here's what I did to add two links inside the div

div(:class => "some_class") do
  [
    link_to("Link1", link1_path),
    link_to("Link2", link2_path)
  ].join.html_safe
end

Behind it does is first, create the link tag in strings inside an array, second, you join these strings and make it html_safe so div can accept it.

vpibano
  • 2,415
  • 1
  • 13
  • 26