【问题标题】:Multiple fields types for nested form嵌套表单的多种字段类型
【发布时间】:2018-11-04 15:57:57
【问题描述】:

我的应用有位置,每个位置都有很多 IP。使用 railscasts 相关的情节 (http://railscasts.com/episodes/197-nested-model-form-part-2) 我建立了一个 location 表单,其中包括 ips 列表和添加新列表的选项。每个ip 都有由表单生成的text_field。

问题是,我希望现有的 ips 显示为 labels 而不是可编辑的文本框,如下所示:

  • 现有(只读)

  • 现有(只读)

  • 现有(只读)

新链接

点击链接后,会附加一个可编辑的文本框。有什么想法吗?


模型:

location.rb:

class Ip < ActiveRecord::Base
  belongs to :location
  validates_uniqueness_of :address
  [...]
end

ip.rb:

class Location < ActiveRecord::Base
  has_many :ips
  accepts_nested_attributes_for :ips
  [...]
end

控制器:

class LocationsController < ApplicationController
  [...]
  def location_params
    params.require(:location).permit(:name, ips_attributes: [:id, :address])
  end
end

助手:

module ApplicationHelper
  [...]  
  def link_to_add_fields(name, f, association)
    new_obj = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_obj, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to name, "#", onclick: "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"
  end
end

观看次数:

/locations/_form.html.erb:

<header>
  <script>
    function add_fields(link, association, content) {
      var new_id = new Date().getTime();
      var regexp = new RegExp("new_" + association, "g")
      $(link).parent().before(content.replace(regexp, new_id));
    }
  </script>
</header>
<%= form_for @location do |f| %>
  [...]
  <%= f.label :ips, t('location.ips') %>
  <%= f.fields_for :ips do |builder| %>
    <%= redner "ip_fields", f: builder %>
  <% end %>
  <p>
    <%= link_to_add_fields t('location.add_ip'), f, :ips %>
  </p>
  [...]
<% end %>

_ip_fields.html.erb:

<p class="fields">
  <%= f.text_field :address %>
</p>

相关的 HTML:

  <label for="location_ips">Addresses</label>

    <p class="fields">
    <input type="text" value="1.2.3.5" name="location[ips_attributes][0][address]" id="location_ips_attributes_0_address" />
</p>

<input type="hidden" value="19" name="location[ips_attributes][0][id]" id="location_ips_attributes_0_id" />
    <p class="fields">
    <input type="text" value="2.3.4.5" name="location[ips_attributes][1][address]" id="location_ips_attributes_1_address" />
</p>

<input type="hidden" value="20" name="location[ips_attributes][1][id]" id="location_ips_attributes_1_id" />
    <p class="fields">
    <input type="text" value="3.4.5.6" name="location[ips_attributes][2][address]" id="location_ips_attributes_2_address" />
</p>

<input type="hidden" value="21" name="location[ips_attributes][2][id]" id="location_ips_attributes_2_id" />
    <p class="fields">
    <input type="text" value="9.9.9.9" name="location[ips_attributes][3][address]" id="location_ips_attributes_3_address" />
</p>

<input type="hidden" value="22" name="location[ips_attributes][3][id]" id="location_ips_attributes_3_id" />
  <p>
    <a onclick="add_fields(this, &quot;ips&quot;, &quot;&lt;p class=\&quot;fields\&quot;&gt;\n  &lt;input type=\&quot;text\&quot; name=\&quot;location[ips_attributes][new_ips][address]\&quot; id=\&quot;location_ips_attributes_new_ips_address\&quot; /&gt;\n&lt;\/p&gt;\n&quot;)" href="#">add new IP address</a>
    </p>

【问题讨论】:

  • 你能从浏览器显示相关的html sn-p吗?
  • @DominiqueFortin 完成
  • 有些奇怪。如何从new Date().getTime() 获得new_ips 的0、1、2 和3。
  • 我要做的是生成&lt;span&gt;...&lt;/,后跟&lt;input type="hidden" ...&gt; 用于只读内容,&lt;input type="text" ...&gt; 用于新内容。

标签: javascript ajax forms ruby-on-rails-4 nested-forms


【解决方案1】:

嗯,这并不漂亮(不推荐视图中的逻辑),但它提供了我想要的解决方案:辅助方法中的一个额外参数来表示未标记的字段,然后重构部分条件这个变量的存在

帮手:

module ApplicationHelper
  [...]  
  def link_to_add_fields(name, f, association)
    new_obj = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_obj, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder, labelled: false)
    end
    link_to name, "#", onclick: "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"
  end
end

_ip_fields.html.erb:

<p class="fields">
  <% if (defined? labelled) %>
    <%= f.text_field :address %>
  <% else %>
    <%= f.label :address, f.object.address %>
  <% end %>
</p>

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多