带字符串的simple_form集合

时间:2014-06-11 13:23:05

标签: ruby-on-rails ruby simple-form

我试图为简单表单设置收集方法,它就像这样工作

def collection_input(attributes)
    options = "<option value = 1>enter text</option>"
    attributes.each do |attribute|
        if attribute == "selected"
            options = options + "<option value = '1' selected>#{attribute}</option>"
        else
            options = options + "<option value = '1'>#{attribute}</option>"
        end
    end
    options.html_safe
end

我的结果几乎是我需要的,除了我得到一个额外的选项&#39;这个options字符串字面意思是作为值放入。我不需要那个:)我如何使用带有选项的simple_form集合作为字符串?

修改

我想要的是什么:

<option value = '1'>enter text</option>
<option value = '0' selected>attr1</option>
<option value = '0'>attr2</option>
<option value = '0'>attr3</option>
<option value = '0'>attr4</option>

我得到了什么:

<option value = "<option value = '1'>enter text</option>
<option value = '0' selected>attr1</option>
<option value = '0'>attr2</option>
<option value = '0'>attr3</option>
<option value = '0'>attr4</option>"></option>

<option value = '1'>enter text</option>
<option value = '0' selected>attr1</option>
<option value = '0'>attr2</option>
<option value = '0'>attr3</option>
<option value = '0'>attr4</option>

0 个答案:

没有答案