Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement remote: true functionality without link_to?

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?

like image 273
Rajkaran Mishra Avatar asked Oct 13 '17 15:10

Rajkaran Mishra


1 Answers

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.

like image 120
kolas Avatar answered Oct 21 '22 02:10

kolas