0

使用 Ruby on Rails 3.2。我正在尝试基于定义的数组创建多个隐藏字段标记。下面是我的代码:

# shop.rb
...
ADDRESS_COMPONENTS = ['lat', 'lng', 'name']
...

# shops_helper.rb
module ShopsHelper
  def address_components_hidden_fields
    Shop::ADDRESS_COMPONENTS.each do |address_component|
      hidden_field_tag(address_component, "")
    end
  end
end

# _form.html.erb
<%= address_components_hidden_fields %>

输出是:

['lat', 'lng', 'name']

我期望输出是:

<input type='hidden' name='lat' id='lat' value='' />
<input type='hidden' name='lng' id='lng' value='' />
<input type='hidden' name='name' id='name' value='' />

我做错了什么?

4

1 回答 1

0

您需要将hidden_field_tag助手连接在一起。

module ShopsHelper
  def address_components_hidden_fields
    Shop::ADDRESS_COMPONENTS.map do |address_component|
      hidden_field_tag(address_component, "")
    end.join("\n")
  end
end

map注意和的用法join

于 2012-10-02T16:42:19.937 回答