Rails 3 form question: How to create a "select" with the option "Add new ..."?

I would like to create a box selectwith existing brands (e.g. Sony, Panasonic, etc.). In addition, I would like to have a parameter Add New Brand, for example, when the user clicks this option, a new text field appears.

Are there any helper methods in Rails 3 that do such a thing, or do I need to implement this myself using Javascript?

+3
source share
2 answers

To my knowledge, there is no such helper method.

Here is how I would do it in JS:

document.getElementById('someSelectBox').onchange = function() {
    if(this.selectedIndex != this.options.length -1) return;
    var new_name = prompt('Please enter a name');
    if(!new_name.length) return;
    var textbox = document.createElement('input');
    textbox.value = new_name;
    this.parentNode.appendChild(textbox); //parentNode is presumably the form
}

: http://jsfiddle.net/tCBqA/

+6

Source: https://habr.com/ru/post/1779784/


All Articles