Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .- Gemas utilizadas
- 'simple_form'
- 'select2-rails'
- ______________ View
- <%= simple_form_for @model, validate: true do |f| %>
- <%= f.label :customers, 'Clientes' %>
- <%= f.collection_select :customer_ids, @producers, :id, :name_with_country, { :prompt => '- Select -' }, { :multiple => true, class: 'js-add-customers' } %>
- <%= f.submit "Guardar", class: "btn btn-block btn-primary" %>
- <% end %>
- ______________ CoffeeScript
- $('.js-add-customers').select2
- ajax:
- url: '/controller/customers.json'
- dataType: 'json'
- delay: 250
- data: (params) ->
- {
- q: params.term
- page: params.page
- }
- processResults: (data, page) ->
- { results: $.map(data, (customer, i) ->
- {
- id: customer.id
- text: customer.name
- }
- ) }
- cache: true
- theme: 'bootstrap'
- minimumInputLength: 1
- maximumInputLength: 20
- 'width': '100%'
- ______________ Controller
- def customers
- @customers = Customer.where('unaccent(lower(name) )LIKE unaccent(?)', "%#{params[:q].downcase}%")
- respond_to do |format|
- format.json { render json: @customers.map { |customer| { id: customer.id, name: customer.name_with_country } } }
- end
- end
Add Comment
Please, Sign In to add comment