Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. <%= f.text_field :name, required: '', 'ng-model': 'html.name', 'unique-name': '', 'ng-model-options': '{ debounce:500 }' %>
  2.  
  3. app.directive('uniqueName', function ($http, $q) {
  4. return {
  5. require: 'ngModel',
  6. link: function(scope, element, attrs, ngModel) {
  7. ngModel.$asyncValidators.unique = function (name) {
  8. var id = document.getElementById('id');
  9. var path = location.pathname;
  10. return $http.get(path.substr(0, path.indexOf('/', 8) + 1)+'verify?name='+name+'&id='+(id && id.value ? id.value : 0)).then(
  11. function (response) {
  12. return response.data.error ? $q.reject() : true;
  13. }
  14. );
  15. };
  16. }
  17. };
  18. });
  19.  
  20. def verify
  21. render json: Admin::Html.select(:name).where(name: params[:name]).where.not(id: params[:id]).empty? ? { success: 'Record not found.' } : { error: 'Record found.' }
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement