Skip to content

Instantly share code, notes, and snippets.

@jwo
Created March 17, 2014 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwo/9604213 to your computer and use it in GitHub Desktop.
Save jwo/9604213 to your computer and use it in GitHub Desktop.
Possible Additions to Chapter 7.2 of Angularails
<div ng-app="AngulaRails" ng-controller="WidgetController" ng-init="setWidget(<%= @widget.to_json %>);">
<%= form_for @widget, id: "widgetForm", html: { name: "widgetForm", "novalidate" => true, "ar-submit" => true } do |f| %>
<% if @widget.errors.any? %>
<div class="alert alert-danger">
<h4><%= pluralize(@widget.errors.count, "error") %></h4>
<ul>
<% @widget.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group" ng-class="{ 'has-error': widgetForm['widget[name]'].$invalid }">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", "ng-model" => "widget.name", required: true, "ng-minlength" => "7" %>
<span class="help-block" ng-show="widgetForm['widget[name]'].$error.required">Required</span>
<span class="help-block" ng-show="widgetForm['widget[name]'].$error.minlength">Minimum length of 7</span>
</div>
<div class="form-group" ng-class="{ 'has-error': widgetForm['widget[price]'].$invalid }">
<%= f.label :price %>
<%= f.number_field :price, class: "form-control", "ng-model" => "widget.price", required: true, "min" => "10" %>
<span class="help-block" ng-show="widgetForm['widget[price]'].$error.required">Required</span>
<span class="help-block" ng-show="widgetForm['widget[price]'].$error.min">Most be greater than 10</span>
</div>
<%= f.submit "Save", class: "btn btn-primary", "ng-disabled" => "widgetForm.$invalid" %>
<% end %>
</div>
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require angular
//= require angular-application
AngulaRails.controller "WidgetController", ($scope) ->
$scope.widget = {}
$scope.setWidget = (record) ->
$scope.widget = record
@jwo
Copy link
Author

jwo commented Mar 17, 2014

  1. Remove Turbolinks, as it caused problems...
  2. Since we are editing a form, you'll want to bet that form to the controller using ng-init. We added the setWidget method in the WidgetController to set the widget
  3. change the "ng-model" to "widget.name" instead of "name" to link to the object we set in the controller
  4. Add ng-controller="WidgetController" to specify the controller we want to link to

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