Bootstrap 3, Haml, and Rails 4 - Create dropdown from model attributes -
i have 2 models, notification , notificationpath.
each notification has area attribute maps notificationpath areas.
so notificationpath has_many notifications.
i have following haml code in notification/new view:
= f.input :area, :required => false, label: false, :autofocus => true, placeholder: 'notification area', input_html: { class: 'form-control' } this creates text field can type into, , functions correctly. however, want display dropdown of of available notificationpath areas. when select choice, dropdown box should filled selected text.
how can using haml , bootstrap 3?
use collection_select tag or select_tag create dropdown as:
= collection_select :dropdown, :notification_id, notification.all, :id, :area, { allow_blank: "select area..." }, { class: 'dropdown_notification' } note have used dropdown first parameter instead of :notification or f.collection_select, because do not want parameter nest :notification parameters hash.
from markup, note down id of field, must dropdown_notification_id.
in javascript/coffescript, write function track change in dropdown, using change populate input text_field as:
jquery -> $('form').on 'change', '.dropdown_notification', (event) -> #console.log(this) selected_value = $(this).children().filter(":selected").text() #now fill value in input field as: $("input#notification_area").val(selected_value) the code not fullproof since wrote intuition, should give clear picture of may want achieve it.
hope helps :)
luck!
Comments
Post a Comment