[英]How to iterate inside respond_to according to rails variables?
I'm looking for a way to display multiple forms in a table when a new Cars is saved. 我正在寻找一种保存新车时在表格中显示多个表格的方法。
I don't know how to iterate with Cars Options in order to pass Cars & Options parameters to Values form inside Cars create.js.erb 我不知道如何使用Cars Options进行迭代,以便将Cars&Options参数传递给Cars create.js.erb中的Values表单
cars.rb cars.rb
def create
...
if @car.save
@fleet.options.each do |option|
new_value = Value.new(car_id: @car.id, option_id: option.id)
new_value.save
@options_values << [option.id, new_value.id]
end
respond_to do |format|
format.html { redirect_to edit_fleet_path(@fleet) }
format.js
end
...
end
I try, each time a car is created, to display a new column in a table and to display for each existing option (which are table's rows), displaying the corresponding value form. 每次创建汽车时,我都会尝试在表格中显示新列,并为每个现有选项(表格的行)显示相应的值形式。
create.js.erb create.js.erb
<% if @car.errors.any? %>
$("#newCarForm").html("<%= j render 'cars/form', fleet: @fleet, car: @car %>"); // => it's ok
<% else %>
$("#newCarForm").before("<td></td>"); // => it's ok
$("#carTitleRow th:last-child").after("<th><%= @car.title %></td>"); // => it's ok
$("#carUrlRow td:last-child").after("<td><%= @car.url %></td>"); // => it's ok
// MY PROBLEM IS BELOW
var newcell = $("<td>Hello</td>")
$("*[id^='optionRow-']").append(newcell);
<% end %>
Each #optionRow-XXXX
XXXX is the corresponding option's id. 每个#optionRow-XXXX
XXXX是相应选项的ID。 I have same ids in @options_values array 我在@options_values数组中有相同的ID
I'd like to be able to iterate in order to, for each #optionRow-XXXX
, looking for right option and value, pass them in params to 'values/form' and finally have something like that: 我希望能够进行迭代,以便为每个#optionRow-XXXX
寻找正确的选项和值,然后将它们以params的形式传递给“ values / form”,最后得到类似的内容:
var newcell = $("<td><%= j render 'values/form', option: @option, value: @value %></td>")
During my tests I had these problems: 在测试期间,我遇到了以下问题:
@option_values.each
without success 我试图使用@option_values.each
没有成功 You have to pass those variables with the locals object I think. 我认为您必须将这些变量与locals对象一起传递。 So instead of 所以代替
$("<td><%= j render 'values/form', option: @option, value: @value %>
do 做
$("<td><%= j render 'values/form', locals: {option: @option, value: @value} %></td>")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.