从 Rails Helper 返回多个标签的最佳方法是什么?

发布于 2024-07-05 09:05:05 字数 339 浏览 6 评论 0原文

我想创建一个隐藏字段并在一个助手中创建一个链接,然后将两者输出到我的 erb。

<%= my_cool_helper "something", form %>

应该输出

link_to "something", a_path
form.hidden_field "something".tableize, :value => "something"

帮助器的定义是什么样子的结果? link_to 和 form.hidden_​​field 的细节并不重要。 重要的是,如何返回两个不同调用的输出。

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 %>

Should out put the results of

link_to "something", a_path
form.hidden_field "something".tableize, :value => "something"

What would the definition of the helper look like? The details of what link_to and the form.hidden_field don't really matter. What matters is, how do I return the output from two different calls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

咋地 2024-07-12 09:05:05

做这件事有很多种方法。

请记住,现有的 Rails 助手(例如 link_to 等)仅输出字符串。 您可以将字符串连接在一起并返回(如果事情很简单,这就是我大多数时候所做的)。

EG:

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

如果您正在做更复杂的事情,您可以将该代码放入部分代码中,然后让您的助手调用 render :partial

如果您正在做比这更复杂的事情,那么您可能需要查看 errtheblog 的 block_to_partial 帮助器,这非常酷

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

夏末染殇 2024-07-12 09:05:05

到目前为止,我想出的最好的方法是:

def my_cool_helper(name, form)
  out = capture { link_to name, a_path }
  out << capture { form.hidden_field name.tableize, value => 'something' }
end

有更好的方法吗?

So far the best I have come up with is:

def my_cool_helper(name, form)
  out = capture { link_to name, a_path }
  out << capture { form.hidden_field name.tableize, value => 'something' }
end

Is there a better way?

梦途 2024-07-12 09:05:05

使用safe_join

我通常更喜欢仅与 + 连接,如 Orion Edwards 的答案 所示,但这里还有另一个选择我最近发现。

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

它的优点是明确列出所有元素以及这些元素的连接。

我发现对于长元素,+ 符号可能会在行尾丢失。 此外,如果您要连接多个元素,我发现将它们列在这样的数组中对于下一个读者来说会更明显。

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.

放肆 2024-07-12 09:05:05
def output_siblings
  div1 = tag.div 'some content'
  div2 = tag.div 'other content'

  div1 + div2
end

只是在这里简化一些其他答案。

def output_siblings
  div1 = tag.div 'some content'
  div2 = tag.div 'other content'

  div1 + div2
end

just simplifying some other answers in here.

毁梦 2024-07-12 09:05:05

这对我有用。

def format_paragraphs(text)
  text.split(/\r?\n/).sum do |paragraph|
    tag.p(paragraph)
  end
end

This worked for me.

def format_paragraphs(text)
  text.split(/\r?\n/).sum do |paragraph|
    tag.p(paragraph)
  end
end
转角预定愛 2024-07-12 09:05:05

如果您想缓冲除字符串之外的其他输出,则可以使用 concat 而不是 +
看到这个 - http://thepugautomatic.com/2013/06/helpers/

If you want to buffer other output which apart from string then you can use concat instead of +.
see this - http://thepugautomatic.com/2013/06/helpers/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文