简体   繁体   中英

How to store the dropdown, other option text input data in database using ruby on rails?

I do have a select dropdown and in that few options also and in that OTHERS option also

<div class="form-group clearfix">
    <div class="col-sm-2" id="mainone">
        <%#= form.text_field :city, placeholder: "CITY", class: 'form-control required' %>
        <%= form.select :city, options_for_select([['DELHI'],['GURGAON'],['FARIDABAD'],['GHAZIABAD'],['NOIDA'],['MUMBAI'],['THANE'], ['BANGALORE'],['OTHERS']]), {include_blank: 'CITY*'}, class: 'form-control required', name: 'city', :onchange => 'Checkselectedone(this.value);' %>
            <% if @ezetab.errors[:city].present? %>
                <span class="error_msg"><%= @ezetab.errors[:city][0]%></span>
            <% end %>
    </div>
</div>
<div class="form-group clearfix">
    <div class="col-sm-2">
        <%= form.text_field :city, name: 'city', id: 'newtext', label: "Others",style: 'display:none;' %>
    </div>
</div>

when i select the others option one more text box should come and i can type which is my city.

but the thing is here that entered city data also should be store in the same filed. but i dont no how to save that one in the same field.

these are the my fields

def ezetab_params
        params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
    end

this is javascript code

<script type="text/javascript">
function Checkselectedone(val){
    alert("one more text box is coming");
 var element=document.getElementById('newtext');
 if(val=='OTHERS')
   element.style.display='block';
 else  
   element.style.display='none';
}

</script>

can any one tell me how to store that other option data also in the same city filed and i have give required also for that it should not be shown any validation msg. please let me know.
thanks in advance
<div class="form-group clearfix">
    <div class="col-sm-2" id="mainone">
        <%#= form.text_field :city, placeholder: "CITY", class: 'form-control required' %>
        <%= form.select :city, options_for_select([['DELHI'],['GURGAON'],['FARIDABAD'],['GHAZIABAD'],['NOIDA'],['MUMBAI'],['THANE'], ['BANGALORE'],['OTHERS']]), {include_blank: 'CITY*'}, class: 'form-control required', :onchange => 'Checkselectedone(this.value);' %>
            <% if @ezetab.errors[:city].present? %>
                <span class="error_msg"><%= @ezetab.errors[:city][0]%></span>
            <% end %>
    </div>
</div>
<div class="form-group clearfix">
    <div class="col-sm-2">
        <%= text_field_tag :others, "", class: 'form-control', id: 'newtext', style: 'display:none;' %>
    </div>
</div>

Controller: -

def ezetab_params
   ep = params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
   ep[:city] = params[:others] if params[:others].present?
   ep            
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM