I am trying to implement nested models, here is the route file entry:
resources :projects do
resources :instances
end
Following is the snippet for project controller:
# GET /projects/new
def new
@project = Project.new
@project.instances.build
end
and project's form view:
<%= simple_form_for(@project) do |f| %>
...
<%= label_tag :instance_count, "Instance Count" %>
<%= select_tag :instance_count, options_for_select([0, 1, 2, 3, 4, 5], 0) %>
...
<% end %>
Now when I change the number of instance count, I need to display instance fields those many times below the above form. Here is the partial code for that:
<%= form.simple_fields_for :instances do |i| %>
...
<% end %>
Basically I need to call <%= render 'instances/form', form: f %>
from project's javascript file. It should work like link with remote: true
option. But in this case there is no link, but on change event the form need to be displayed. How should I implement this?
I suggest you to use https://github.com/nathanvda/cocoon
Or you can use similar aproach: render partial in initial form (with display:none), then remove and save partial fields with js and clone them to form when selector is hit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With