从Rails Helper返回多个标签的最佳方法是什么? [英] What is the best way to return multiple tags from a Rails Helper?

查看:65
本文介绍了从Rails Helper返回多个标签的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

应该把结果

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

助手的定义是什么样的? link_to和form.hidden_​​field的详细信息并不重要.重要的是,如何从两个不同的调用返回输出.

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.

推荐答案

有几种方法可以做到这一点.

There are several ways to do this.

请记住,现有的rails辅助程序(例如link_to等)仅输出字符串.您可以将字符串连接在一起并返回该字符串(如果事情很简单,这是我大部分时间所做的事情).

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')

如果您要做的事情更复杂,则可以将该代码放一部分,然后让助手调用render :partial.

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.

如果您正在做的事情比这还要复杂,那么您可能想看看errtheblog的

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

这篇关于从Rails Helper返回多个标签的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆