简体   繁体   English

如何使用Rails在数据库中存储下拉列表和其他选项文本输入数据?

[英]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 这是JavaScript代码

<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails Form 添加带有文本框的“其他”选项到 Select 下拉列表 - Ruby on Rails Form Add an "Other" Option with Text Box to Select Dropdown 当使用Active Admin在Ruby on Rails中选择了下拉菜单中的项时,如何填充其他表单输入 - How to fill other form input when an item from dropdown has been selected in Ruby on Rails using Active Admin 如何在Ruby on Rails应用程序数据库中存储ldap数据? - how to store ldap data in ruby on rails application database? 使用下拉列表在Ruby on Rails中过滤数据库结果 - Filtering Database results in Ruby on Rails using dropdown 如何使用Ruby on Rails将字符串存储为数据库列中的数组 - How to store string as array in database column using Ruby on Rails 如何使用ruby将其他语言存储在mysql数据库中? - How to store other languages in mysql database using ruby? 将数据存储在没有数据库的Ruby on Rails中 - Store data in Ruby on Rails without Database 如何在不使用Rails的循环的情况下将文本字段数组数据存储在数据库中 - How to Store the text fields array data in database without using loop using rails 如何在 Ruby on Rails 中为下拉列表的文本着色 - How to color text of dropdown list in Ruby on Rails 在表单提交Rails之前如何处理文本输入数据 - How to process text input data before form submit ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM