Skip to content

Instantly share code, notes, and snippets.

@tatey
Created January 13, 2013 04:56
Show Gist options
  • Save tatey/4522385 to your computer and use it in GitHub Desktop.
Save tatey/4522385 to your computer and use it in GitHub Desktop.
Rails ERB template for http://jsfiddle.net/tatejohnson/vLhNb/. Adding and removing children to a Rails nested form with AngularJS.
<h1>New Plan</h1>
<div ng-app>
<%= form_for @plan do |form| %>
<div ng-controller="PollCtrl" ng-init="polls = <%= @plan.polls.to_json %>">
<div ng-repeat="poll in polls">
<%= form.fields_for :polls, Poll.new, child_index: '{{$index}}' do |poll_form| %>
<%= poll_form.text_field :title, id: 'plan_poll_{{$index}}', value: '{{poll.title}}' %>
<% end %>
<a href="#" ng-click="remove($index)" ng-show="isRemovable()">Remove</a>
</div>
<a href="#" ng-click="add()">Add</a>
</div>
<% end %>
</div>
@tatey
Copy link
Author

tatey commented Jan 13, 2013

You need to set an id on the text field, otherwise all your text fields will have plan_polls_attributes____index___title as their id. If you don't care, then you can omit it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment