【问题标题】:How to use a helper method to populate a collection of radio buttons in Rails?如何使用辅助方法填充 Rails 中的单选按钮集合?
【发布时间】:2013-02-21 00:17:40
【问题描述】:

我正在尝试使用单选按钮的集合 (?) 填充表单:

# _fields.html.erb

<%= f.label :invoice_type %><br/>   
<%= radio_tags_for_select(f.object.invoice_types) %>

# application_helper.rb

def radio_tags_for_select(types)   
  types_html = types.map do |type|
    content_tag :span, :class => "radio_option" do
      radio_button(:invoice_types, type, type) + type
    end
  end
  safe_join(types_html)
end

似乎我无法在此行中获得正确的语法:radio_button(:invoice_types, type, type) + type,因为表单已显示但没有保存保存到数据库。

有人可以帮忙吗?

谢谢...

【问题讨论】:

  • 请检查您的参数以获得更好的想法。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 radio-button


【解决方案1】:

我认为你必须将原始字符串转义为 html:

content_tag :span, :class => "radio_option" do
 raw( radio_button(:invoice_types, type, type) + type )
end

【讨论】:

  • 谢谢,但实际上safe_join(types_html) 做得很好。
【解决方案2】:

这就是我最终的结果:

def radio_tags_for_select(types, f)   
  types_html = types.map do |type|
    content_tag :span, :class => "radio_option" do
      f.radio_button(:invoice_type, type) + localize_address_type(type)
    end
  end
  safe_join(types_html)
end

之前好像忘记传递对象f了。

如果可以简化此代码,请告诉我。我愿意接受建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多