如何更改 fields_for 生成的 html 标签和类?

发布于 2024-12-28 03:04:33 字数 1611 浏览 1 评论 0原文

这是一个简单的问题,我有点羞于问,但我一直在用头撞墙并浏览 Rails 3 文档,但没有成功:/

所以,事情是这样的:

当我使用 fields_for 帮助器它将生成的字段包装在

中...
标签。

所以,我的代码是

<ul class="block-grid two-up">
  <%= f.fields_for :images do |image_builder| %>
    <%= render "images/form", :f => image_builder %>
  <% end %>
</ul>

,生成的html是:

<ul class="block-grid two-up">
  <div class="fields">
    <div>
      <label for="company_images_attributes_0_image"> Image</label>
      <input id="company_images_attributes_0_image" 
             name="company[images_attributes][0][image]" type="file">
    </div>
  </div>
  <div class="fields">
    <div>
      <label for="company_images_attributes_1_image"> Image</label>
      <input id="company_images_attributes_1_image" 
             name="company[images_attributes][1][image]" type="file">
    </div>
  </div>
</ul>

我想做的实际上是将

包装标签更改为
  • 文档说你可以将选项传递给 fields_for,但不清楚你可以传递哪些选项,也许你可以更改这个包装标签?

    一种可能是当表单中出现错误时重写一个函数,有点像 ActionView::Base.field_error_proc

    快速编辑:我忘了提及我正在使用 simple_form 来生成此表单。我尝试在 simple_form.rb 配置文件中查找自定义此操作的方法,但我没有看到任何方法。

    解决方案 经过进一步调查,发现该表单还使用 nested_form gem 来生成表单(不仅仅是 simple_form)。该生成器导致 fields_for 被包装在 div 标签中。感谢大家的建议!

    This is a simple question that I'm kinda ashamed to ask, but I've been banging my head against the wall and navigating through the rails 3 documentation without any success :/

    So, here is the thing:

    When I use the fields_for helper it wraps the generated fields in a <div class="fields"> ... </div> tag.

    so, my code is

    <ul class="block-grid two-up">
      <%= f.fields_for :images do |image_builder| %>
        <%= render "images/form", :f => image_builder %>
      <% end %>
    </ul>
    

    and the generated html is:

    <ul class="block-grid two-up">
      <div class="fields">
        <div>
          <label for="company_images_attributes_0_image"> Image</label>
          <input id="company_images_attributes_0_image" 
                 name="company[images_attributes][0][image]" type="file">
        </div>
      </div>
      <div class="fields">
        <div>
          <label for="company_images_attributes_1_image"> Image</label>
          <input id="company_images_attributes_1_image" 
                 name="company[images_attributes][1][image]" type="file">
        </div>
      </div>
    </ul>
    

    What I want to do is actually change the <div class="fields"> wrapper tag to <li>.

    The documentation says you can pass options to the fields_for, but its not clear about what options you can pass, maybe you can change this wrapper tag?

    A possibility could be to override a function, kinda like ActionView::Base.field_error_proc when there is an error in the form.

    Quick edit: I forgot to mention that I'm using simple_form to generate this form. I tried looking in the simple_form.rb config file for a way to customize this, but I didn't see any way of doing it.

    Solution
    After further investigation, it turns out the form was using the nested_form gem as well to generate the form (not only simple_form). This generator was causing the fields_for to be wrapped in the div tag. Thanks everybody for their suggestions!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    如日中天 2025-01-04 03:04:33

    以下禁用包装器:

    f.fields_for :images, wrapper:false do |image_builder|
    

    然后您可以在构建器块中添加您自己的包装器。

    The following disables the wrapper:

    f.fields_for :images, wrapper:false do |image_builder|
    

    then you can add your own wrapper in the builder block.

    桃气十足 2025-01-04 03:04:33

    一个廉价的解决方案只是将

  • 标签添加到表单中,例如:
  • <%= f.fields_for :images do |image_builder| %>
     <li><%= render "images/form", :f => image_builder %></li>
    <% end %>
    

    我不确定是否可以通过将一些参数传递给 field_for 来完全消除 div 标签。但我认为你可以通过传递 html 块来更改 div 类或 id 的名称,例如 form_for 中:

    <%= form_for @image, :as => :post, :url => post_image_path, 
       :html => { :class => "new_image", :id => "new_image" } do |f| %>
    

    A cheap solution would be just adding <li> tag into the form like:

    <%= f.fields_for :images do |image_builder| %>
     <li><%= render "images/form", :f => image_builder %></li>
    <% end %>
    

    I am not sure if you can completely eliminate the div tag by passing some params to field_for. But I think you can change the name of div class or id by passing the html block, like in form_for:

    <%= form_for @image, :as => :post, :url => post_image_path, 
       :html => { :class => "new_image", :id => "new_image" } do |f| %>
    
    入画浅相思 2025-01-04 03:04:33

    你说你正在使用 simple_form 那么你应该说 <%= f.simple_fields_for.. 。您是否尝试过使用 wrapper_html 选项:

    <%= f.input :name, :label_html => { :class => 'upcase strong' },
      :input_html => { :class => 'medium' }, :wrapper_html => { :class => 'grid_6 alpha' } %>
    

    编辑 1:
    来自 SimpleForm 文档:

    Wrapper

    SimpleForm 允许您添加包含标签的包装器、错误、提示和输入。第一步是配置一个包装标签:

    SimpleForm.wrapper_tag = :p
    

    现在,您不再需要包装您的 f.input 调用:

    <%= simple_form_for @user do |f| %>
      <%= f.input :username %>
      <%= f.input :password %>
      <%= f.button :submit %>
    <% end %>
    

    编辑 2:

    并且有一个配置选项,您可以使用它来说明包装元素要使用什么 css 类:

    config/initializers/simple_form.rb

    # CSS class to add to all wrapper tags.
    config.wrapper_class = :input
    

    You said you're using simple_form then you should be saying <%= f.simple_fields_for... Have you tried using wrapper_html option:

    <%= f.input :name, :label_html => { :class => 'upcase strong' },
      :input_html => { :class => 'medium' }, :wrapper_html => { :class => 'grid_6 alpha' } %>
    

    Edit 1:
    From SimpleForm documentation:

    Wrapper

    SimpleForm allows you to add a wrapper which contains the label, error, hint and input. The first step is to configure a wrapper tag:

    SimpleForm.wrapper_tag = :p
    

    And now, you don't need to wrap your f.input calls anymore:

    <%= simple_form_for @user do |f| %>
      <%= f.input :username %>
      <%= f.input :password %>
      <%= f.button :submit %>
    <% end %>
    

    Edit 2:

    And there is this config option with which you can say what css class to use with the wrapper elements:

    config/initializers/simple_form.rb

    # CSS class to add to all wrapper tags.
    config.wrapper_class = :input
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文