2013-07-15 38 views
0

我試圖創建軌道自定義表單生成器來Rails自定義表單生成器。自text_field

<div class="row collapse"> 
     <div class="large-4 columns"> 
     <%= builder.label :vacancy_title, "Title of the Vacancy", class: "right inline" %> 
     </div> 
     <div class="large-8 columns"> 
     <%= builder.text_field :vacancy_title, class: "tiny" %> 
     </div> 
    </div> 

這段代碼轉換簡單

<%= builder.t_field :vacancies, "Label title" %> 

我想,沒有運氣這個代碼,它只是顯示的標籤。

#form_builders/custom_form_builder.rb 
class CustomFormBuilder < ActionView::Helpers::FormBuilder 
def t_field(name, title, *args) 
    @template.content_tag :div, class: "row collapse" do 
    @template.content_tag :div, class: "large-8 columns" do 
    text_field_tag(name, *args) 
    end 

    @template.content_tag :div, class: "large-4 columns" do 
    @template.content_tag :h5 do 
     label(title, options[:label], class: "right inline") 
    end 
    end 

end 
end 

text_field_tag(name,* args)顯然有些問題,但我不知道如何聲明輸入。我沒有發現任何文檔上rubyapi助手:: FormBuilder

解決

由於列維斯坦利我解決了這個用下面的代碼。我需要將text_field_tag(name, *args)更改爲text_field_tag("#{object_name}[#{name}]")label(title, options[:label], class: "right inline")以及label(name, title, *args, class: "right"),以便窗體可以與嵌套屬性一起正常工作。

class LabeledFormBuilder < ActionView::Helpers::FormBuilder 
    def t_field(name, title, *args) 
    @template.content_tag :div, class: "row collapse" do 
     (@template.content_tag :div, class: "large-4 columns" do 
     @template.content_tag :h5 do 
      label(name, title, *args, class: "right") 
     end 
     end) + 
     (@template.content_tag :div, class: "large-8 columns" do 
     @template.text_field_tag("#{object_name}[#{name}]") 
     end) 
    end 
    end 
end 

回答

1

您需要將content_tag s連接到內部div。 content_tag方法使用塊的返回值來確定其內容。您正在運行包含text_field_tag的div的代碼,但實際上並未將其包含在外部「行摺疊」div中,因爲它未包含在該塊的返回值中。

#form_builders/custom_form_builder.rb 
class CustomFormBuilder < ActionView::Helpers::FormBuilder 
    def t_field(name, title, *args) 

    @template.content_tag :div, class: "row collapse" do 
     (@template.content_tag :div, class: "large-8 columns" do 
     text_field_tag(name, *args) 
     end) + 

     (@template.content_tag :div, class: "large-4 columns" do 
     @template.content_tag :h5 do 
      label(title, options[:label], class: "right inline") 
     end 
     end) 
    end 
    end 
end 
+0

列維您好,非常感謝您,但這返回「custom_form_builder.rb:8:語法錯誤,意想不到的‘+’,keyword_end期待」後,我重新啓動服務器導軌 –

+0

謝謝,李維斯,我解決了這個感謝您建議(編輯我的問題)。我需要爲兩個塊添加括號 –