gpt4 book ai didi

ruby-on-rails - 以 rails 形式在循环内嵌套属性的字段

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:23 25 4
gpt4 key购买 nike

我尝试做一些类似 google my business 的事情,在那里你有星期几,你可以添加多次。我关注rail cast nested model form ,但我无法让它按照我想要的方式工作。

我想要一个包含一周 7 天的位置,您可以在每一天添加多个时间。 Create 方法似乎按预期工作,因为表单是从头开始的。然后当我进入编辑时,每天都有创建时保存的所有时间。

我只想在这个问题上得到一些指导。谢谢!

_from.html.erb

<% (0..6).each do |idx| %>
<div class="row">
<div class="col-md-2">
<p><% I18n.t('date.day_names')[idx] %></p>
</div>
<div class="col-md-5">
<%= form.fields_for :times do |f| %>
<%= render "time_fields", :form => f %>
<% end %>
</div>
<div class="col-md-3">
<%= link_to_add_fields "Add Time", "btn", "repeater-add", form, :times %>
</div>
</div>
<% end %>

_time_fields.html.erb

<div class="weekday-time" id="time">
<%= form.hidden_field :weekday_id, value: form.options[:child_index] %>
<div class="col-md-9">
<div class="input-group bootstrap-timepicker timepicker">
<%= form.text_field :time, id: "timepicker" %>
<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
</div>
<div class="col-md-3">
<%= link_to_remove_fields "Delete Time, "btn", "repeater-remove", form %>
</div>
</div>
</div>

Controller

def new
@location = Location.new
@location.times.build
end

模型

class Time < ApplicationRecord
belongs_to :location, optional: true
end

class Location < ApplicationRecord
has_many :times, dependent: :destroy
accepts_nested_attributes_for :times
end

编辑

在尝试了不同的事情之后,我得到了我想要的结果。但是,不确定这是否是最好的方法。

application_helper.rb

module ApplicationHelper
def link_to_remove_fields(name, html_class, data_attr, html_id, form)
form.hidden_field(:_destroy) + link_to_function(name, html_class, html_id, data_attr, "remove_fields(this)")
end

def link_to_add_fields(name, html_class, html_id, data_attr, form, association, idx)
new_object = form.object.class.reflect_on_association(association).klass.new
fields = form.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :form => builder, idx: idx)
end
link_to_function(name, html_class, html_id, data_attr, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end

def link_to_function(name, html_class, html_id, data_attr, *args, &block)
html_options = args.extract_options!.symbolize_keys

function = block_given? ? update_page(&block) : args[0] || ''
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'

content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick, :class => html_class, :id => html_id, :data_attr => data_attr))
end
end

location_controller.rb

def new
@location = Location.new
end

def edit
@times = Times.where(location_id: @location.id)
end

_time_fields.html.erb

<div class="weekday-time" id="time">
<%= form.hidden_field :weekday_id, value: idx %>
<div class="col-md-9">
<div class="input-group bootstrap-timepicker timepicker">
<%= form.text_field :time, id: "timepicker" %>
<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
</div>
</div>
<div class="col-md-3">
<%= link_to_remove_fields "Delete Time, "btn", "plahecolder", "repeater-remove", form %>
</div>
</div>

_form.html.erb

<% (0..6).each do |idx| %>
<div class="row">
<div class="col-md-2">
<p><%= I18n.t('date.day_names')[idx] %></p>
</div>
<div class="col-md-5">
<div id="time<%= idx %>">
<% _current_time = @location.new_record? ? @location.times.build : @times.where(location_id: idx) %>
<% unless _current_time.blank? %>
<%= form.fields_for :times, _current_time do |f| %>
<%= render "time_fields", :form => f, :idx => idx %>
<% end %>
<% end %>
</div>
</div>
<!-- add button -->
<div class="col-md-3">
<%= link_to_add_fields "Add Time", "btn", "repeater-add", "time#{idx}", form, :times, idx %>
</div>
</div>
<% end %>

js

function remove_fields(link) {
$(link).prev("input[type=hidden]").val("true");
$(link).closest(".weekday-time").hide();
}

function add_fields(link, association, content) {
let _attr = $(link).attr('data_attr');
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(content.replace(regexp, new_id)).insertAfter($("#" + _attr));
}

最佳答案

我认为可能还有另一种方法可以做到这一点。如果是这样,请毫不犹豫地指出并回答这个问题。但是,如果有人正在寻找具有类似问题的解决方案,这就是我所做的。

我遍历星期几,然后在 create 上,_current_time 将返回 true,它可以用作 record_object 参数对于 fields_for。它需要一个 record_object 但我在 create 上没有,所以 true 就足够了(不确定 100% 为什么)。然后在 update 上,我遍历有星期几的时间并跳过没有的字段的创建。

_form.html.erb

<% (0..6).each do |idx| %>
<div class="row">
<div class="col-md-2">
<p><%= I18n.t('date.day_names')[idx] %></p>
</div>
<div class="col-md-5">
<div id="time<%= idx %>">
<% _current_time = @location.new_record? ? @location.times.build : @times.where(location_id: idx) %>
<% unless _current_time.blank? %>
<%= form.fields_for :times, _current_time do |f| %>
<%= render "time_fields", :form => f, :idx => idx %>
<% end %>
<% end %>
</div>
</div>
<div class="col-md-3">
<%= link_to_add_fields "Add Time", "btn", "repeater-add", "time#{idx}", form, :times, idx %>
</div>
</div>
<% end %>

添加按钮将在任一情况下使用星期几的适当索引创建新时间。在这个问题的编辑部分,您可以看到完整的实现。只有 _form.html.erb 导致了这个问题,所以我只发布这篇文章作为答案。

关于ruby-on-rails - 以 rails 形式在循环内嵌套属性的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50568810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com