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

后端 未结 5 1460
梦如初夏
梦如初夏 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:32

    There are several ways to do this.

    Remember that the existing rails helpers like link_to, etc, just output strings. You can concatenate the strings together and return that (which is what I do most of the time, if things are simple).

    EG:

    link_to( "something", something_path ) +  #NOTE THE PLUS FOR STRING CONCAT
      form.hidden_field('something'.tableize, :value=>'something')
    

    If you're doing things which are more complicated, you could just put that code in a partial, and have your helper call render :partial.

    If you're doing more complicated stuff than even that, then you may want to look at errtheblog's block_to_partial helper, which is pretty cool

提交回复
热议问题