Ruby Rails自定义表单生成器。自定义文本字段
我试图在rails中创建自定义表单生成器来转换此代码Ruby Rails自定义表单生成器。自定义文本字段,ruby,ruby-on-rails-3,ruby-on-rails-3.2,formbuilder,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,Formbuilder,我试图在rails中创建自定义表单生成器来转换此代码 <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
<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>
您需要将内部div的
content\u标记连接在一起。content_标记方法使用块的返回值来确定其内容。您正在运行包含text\u field\u标记的div的代码,但实际上并未将其包含在外部“row collapse”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
#表单生成器/custom_表单生成器.rb
类CustomFormBuilder
Hello Levi,谢谢,但在我重新启动rails服务器后,返回“custom_form_builder.rb:8:语法错误,意外“+”,需要关键字_end”。谢谢你,Levi,我解决了这个问题,感谢你的建议(编辑了我的问题)。我需要为两个块添加括号
#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
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
#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