Ruby-on-rails 如何将变量值插入到 ERB 模板中的 HTML 标签中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6172174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:16:58  来源:igfitidea点击:

How do I insert variable values into HTML tags in ERB templates?

ruby-on-railsrubyerb

提问by maxfry

I have a partial like:

我有一个部分喜欢:

<% office.map do |o| %>
   <input id='city' name='company[company_office][0][city]' value=.... type='text' />
<% end %>

How can I insert a value like o.officeinto an attribute? value="#{o.office}"does not work.

如何将值插入o.office到属性中?value="#{o.office}"不起作用。

回答by corroded

<% office.map do |o| %>
   <input id='city' name='company[company_office][0][city]' value='<%= o.office %>' type='text' />
<% end %>

or you could use the form helpers for that

或者你可以使用表单助手

回答by ardavis

Use embedded ruby(erb) tags,

使用嵌入的 ruby​​(erb) 标签,

<%= o.office %>

The only time you'd use #{o.office}is when you're not using erb. In a helper method for example and you want to use your ruby in a string. But when you're in an html.erbfile, you have to use the erb tags.

您唯一会使用的时间#{o.office}是不使用erb 时。例如,在辅助方法中,您想在字符串中使用 ruby​​。但是当你在一个html.erb文件中时,你必须使用 erb 标签。