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.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
<% 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. I have same ids in @options_values array
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:
var newcell = $("<td><%= j render 'values/form', option: @option, value: @value %></td>")
During my tests I had these problems:
@option_values.each
without success You have to pass those variables with the locals object I think. So instead of
$("<td><%= j render 'values/form', option: @option, value: @value %>
do
$("<td><%= j render 'values/form', locals: {option: @option, value: @value} %></td>")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.