What is the best way to return multiple tags from a Rails Helper?

后端 未结 5 1457
梦如初夏
梦如初夏 2021-02-02 09:19

I want to create a hidden field and create a link in one helper and then output both to my erb.

<%= my_cool_helper \"something\", form %>

5条回答
  •  清歌不尽
    2021-02-02 09:39

    Using safe_join.

    I typically prefer just concatenating with +, as shown in Orion Edwards's Answer, but here's another option I recently discovered.

    safe_join( [
      link_to( "something", something_path ),
      form.hidden_field( "something".tableize, value: "something" )
    ] )
    

    It has the advantage of explicitly listing all of the elements and the joining of those elements.

    I find that with long elements, the + symbol can get lost at the end of the line. Additionally, if you're concatenating more than a few elements, I find listing them in an Array like this to be more obvious to the next reader.

提交回复
热议问题