2012-05-13 101 views
1

我一直在嘗試我的手在Rails中的自定義窗體生成器方法。我的目標類似於formtasticf.inputs方法。這是我到目前爲止的代碼:content_tag不是從ActionView :: Helpers渲染:: FormBuilder

class Builder < ActionView::Helpers::FormBuilder 

    def fieldset(name) 
    @template.content_tag :fieldset, :class => 'inputs' do 
     @template.content_tag :div, :class => 'legend' do 
     @template.content_tag :span, name 
     end 
     @template.content_tag :ol do 
     yield 
     end 
    end 
    end 

    def input_field(name, classes='', *args) 
    @template.content_tag :li, class: "input #{classes}" do 
     label(name) + text_field(name) 
    end 
    end 

end 

於是我可以在HAML建立形式就像這樣:

= f.fieldset "General Info" do 
    = f.input_field :name 
    = f.input_field :slug, 'last' 

問題是渲染HTML下降傳說DIV。

<fieldset class="inputs"> 
    <ol> 
    <li class="input "> 
     <label for="content_instance_name">Name</label> 
     <input id="content_instance_name" name="content_instance[name]" size="30" type="text" value="Home Page"> 
    </li> 
    <li class="input last"> 
     <label for="content_instance_slug">Slug</label> 
     <input id="content_instance_slug" name="content_instance[slug]" size="30" type="text" value="home-page"> 
    </li> 
    </ol> 
</fieldset> 

好像我失去了一些東西。
在此先感謝。

SOLUTION

下面是調整後字段集方法:

def fieldset(name) 
    @template.content_tag :fieldset, :class => 'inputs' do 
    legend = @template.content_tag :div, @template.content_tag(:span, name), :class => "legend" 
    legend += @template.content_tag :ol do 
     yield 
    end 
    end 
end 

回答

2

只有塊的返回值被呈現。添加+,以便您的content_tag調用返回。 Similar question here.

+0

就是這樣,謝謝。 –

+0

不客氣,現在接受答案!哈哈 –

相關問題