[英]How can I DRY this <div> with Rails Forms?
I understand that with Rails Form Helpers, I can pass in classes, placeholders, types, etc. into Rails, so instead of writing 我了解使用Rails Form Helpers,可以将类,占位符,类型等传递到Rails中,因此无需编写
<input id="name" name="name" type: "text" pattern="" value="Kanye West"/>
<label class="mdl-textfield__label" for="name">Enter Name</label>
I can do: 我可以:
<%= f.text_field :name, class: 'mdl-textfield__input', type: 'text', pattern: '-?[0-9]*(\.[0-9]+)?', id: 'name', value: 'Kanye West' %>
<label class="mdl-textfield__label" for="name">Enter Name</label>
But it becomes pointless, because if I use a CSS framework, it ends up looking like this... 但这变得毫无意义,因为如果我使用CSS框架,它最终将看起来像这样……
<div class="mdl-cell mdl-cell--6-col mdl-cell--4-col-tablet hide-dis">
<div class="mdl-textfield advanced-input mdl-js-textfield mdl-textfield--expandable">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field :name, class: 'mdl-textfield__input', type: 'text', pattern: '-?[0-9]*(\.[0-9]+)?', id: 'name', value: 'Kanye West' %>
<label class="mdl-textfield__label" for="name">Enter Name</label>
</div>
</div>
</div>
You can imagine how ugly this can get when you have 10 form elements, right. 您可以想象,当您有10个表单元素时,这会变得多么丑陋。 I could try partializing it, but I don't know how I would pass in the f.textfield
part. 我可以尝试partializing它,但我不知道我怎么会通过在f.textfield
部分。 Please let me know how I can improve this mess and or anything else that you see that can be improved with me code :) 请让我知道如何改善这种混乱状况, 或者您看到的其他任何可以通过我的代码改善的内容:)
You can create your own FormBuilder and create custom controls that include the wrapper classes than you need. 您可以创建自己的FormBuilder并创建包含所需包装类的自定义控件。
For example if you want to wrap your text fields in a particular div
: 例如,如果您要将文本字段包装在特定的div
:
class CustomFormBuilder < ActionView::Helpers::FormBuilder
def custom_text_field(method, tag_value, options = {})
@template.content_tag(:div,
@template.text_field(
@object_name, method, tag_value, objectify_options(options)
),
class: 'mdl-textfield mdl-js-textfield'
)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.