Guest User

Untitled

a guest
Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. .- Gemas utilizadas
  2. 'simple_form'
  3. 'select2-rails'
  4.  
  5. ______________ View
  6.  
  7. <%= simple_form_for @model, validate: true do |f| %>
  8.  
  9. <%= f.label :customers, 'Clientes' %>
  10. <%= f.collection_select :customer_ids, @producers, :id, :name_with_country, { :prompt => '- Select -' }, { :multiple => true, class: 'js-add-customers' } %>
  11. <%= f.submit "Guardar", class: "btn btn-block btn-primary" %>
  12.  
  13. <% end %>
  14.  
  15.  
  16. ______________ CoffeeScript
  17.  
  18. $('.js-add-customers').select2
  19. ajax:
  20. url: '/controller/customers.json'
  21. dataType: 'json'
  22. delay: 250
  23. data: (params) ->
  24. {
  25. q: params.term
  26. page: params.page
  27. }
  28. processResults: (data, page) ->
  29. { results: $.map(data, (customer, i) ->
  30. {
  31. id: customer.id
  32. text: customer.name
  33. }
  34. ) }
  35. cache: true
  36. theme: 'bootstrap'
  37. minimumInputLength: 1
  38. maximumInputLength: 20
  39. 'width': '100%'
  40.  
  41. ______________ Controller
  42.  
  43. def customers
  44. @customers = Customer.where('unaccent(lower(name) )LIKE unaccent(?)', "%#{params[:q].downcase}%")
  45.  
  46. respond_to do |format|
  47. format.json { render json: @customers.map { |customer| { id: customer.id, name: customer.name_with_country } } }
  48. end
  49. end
Add Comment
Please, Sign In to add comment