如何删除id& rails视图助手的名称属性

时间:2013-07-13 17:28:25

标签: html ruby-on-rails view helper ruby-on-rails-4

默认情况下,Rails会从name参数为select_tag生成id和名称:

select_tag "people", "<option>David</option>".html_safe
# => <select id="people" name="people"><option>David</option></select>

但是如果我们想要生成select而没有idname属性呢?
像这样:

<select><option>David</option></select>

空名称参数不起作用,空的html属性仍然存在:

select_tag "", "<option>David</option>".html_safe
# => <select id name><option>David</option></select>

手动idname分配不起作用

select_tag "people", "<option>David</option>".html_safe, id: false, name: false
# => <select id=false name=false><option>David</option></select>
select_tag "people", "<option>David</option>".html_safe, id: '', name: ''
# => <select id name><option>David</option></select>

1 个答案:

答案 0 :(得分:2)

idname可以通过为其分配nil来删除:

select_tag nil, "<option>David</option>".html_safe, id: nil
# => <select><option>David</option></select>