- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试做一些类似 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/
我是 PHP 新手。我一直在脚本中使用 for 循环、while 循环、foreach 循环。我想知道 哪个性能更好? 选择循环的标准是什么? 当我们在另一个循环中循环时应该使用哪个? 我一直想知道要
我在高中的编程课上,我的作业是制作一个基本的小计和顶级计算器,但我在一家餐馆工作,所以制作一个只能让你在一种食物中读到。因此,我尝试让它能够接收多种食品并将它们添加到一个价格变量中。抱歉,如果某些代码
这是我正在学习的一本教科书。 var ingredients = ["eggs", "milk", "flour", "sugar", "baking soda", "baking powder",
我正在从字符串中提取数字并将其传递给函数。我想给它加 1,然后返回字符串,同时保留前导零。我可以使用 while 循环来完成此操作,但不能使用 for 循环。 for 循环只是跳过零。 var add
编辑:我已经在程序的输出中进行了编辑。 该程序要求估计给定值 mu。用户给出一个值 mu,同时还提供了四个不等于 1 的不同数字(称为 w、x、y、z)。然后,程序尝试使用 de Jaeger 公式找
我正在编写一个算法,该算法对一个整数数组从末尾到开头执行一个大循环,其中包含一个 if 条件。第一次条件为假时,循环可以终止。 因此,对于 for 循环,如果条件为假,它会继续迭代并进行简单的变量更改
现在我已经习惯了在内存非常有限的情况下进行编程,但我没有答案的一个问题是:哪个内存效率更高;- for(;;) 或 while() ?还是它们可以平等互换?如果有的话,还要对效率问题发表评论! 最佳答
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 我正在尝试创建一个小程序,我可以在其中读取该程序的单词。如果单词有 6
这个问题在这里已经有了答案: python : list index out of range error while iteratively popping elements (12 个答案) 关
我正在尝试向用户请求 4 到 10 之间的整数。如果他们回答超出该范围,它将进入循环。当用户第一次正确输入数字时,它不会中断并继续执行 else 语句。如果用户在 else 语句中正确输入数字,它将正
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我想要做的事情是使用循环创建一个数组,然后在另一个类中调用该数组,这不会做,也可能永远不会做。解决这个问题最好的方法是什么?我已经寻找了所有解决方案,但它们无法编译。感谢您的帮助。 import ja
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我正在模拟一家快餐店三个多小时。这三个小时分为 18 个间隔,每个间隔 600 秒。每个间隔都会输出有关这 600 秒内发生的情况的统计信息。 我原来的结构是这样的: int i; for (i=0;
这个问题已经有答案了: IE8 for...in enumerator (3 个回答) How do I check if an object has a specific property in J
哪个对性能更好?这可能与其他编程语言不一致,所以如果它们不同,或者如果你能用你对特定语言的知识回答我的问题,请解释。 我将使用 c++ 作为示例,但我想知道它在 java、c 或任何其他主流语言中的工
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是 C 编程和编写代码的新手,以确定 M 测试用例的质因数分解。如果我一次只扫描一次,该功能本身就可以工作,但是当我尝试执行 M 次时却惨遭失败。 我不知道为什么 scanf() 循环有问题。 in
这个问题已经有答案了: JavaScript by reference vs. by value [duplicate] (4 个回答) 已关闭 3 年前。 我在使用 TSlint 时遇到问题,并且理
我尝试在下面的代码中添加 foreach 或 for 循环,以便为 Charts.js 创建多个数据集。这将允许我在此折线图上创建多条线。 我有一个 PHP 对象,我可以对其进行编码以稍后填充变量,但
我是一名优秀的程序员,十分优秀!