我是ruby on rails的新手,在这里我做了hava下拉选择框 如果我选择任何一个它应该存储的几个选项,如果 我选择其他人意味着应该出现另外一个文本框 值应该存储在数据库中
ezetabs_controller.rb
def create
@ezetab = Ezetab.new(ezetab_params)
if @ezetab.save
redirect_to @ezetab
else
render 'new'
end
end
private
def ezetab_params
ep = params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
ep[:city] = params[:others] if params[:others].present?
ep
end
ezetab.rb
validates :city, presence: true
new.html.erb
<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 :others, "", id: 'newtext',style: 'display:none;' %>
<%= text_field_tag :others, "", class: 'form-control', id: 'newtext', style: 'display:none;' %>
</div>
</div>
<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';
}
任何人都可以告诉我如何存储选中的下拉菜单 因为我只能存储其他oprtion text_field 选择下拉我无法选择请让我知道,因为我 我是从昨天开始完成这一次,感谢提前
答案 0 :(得分:0)
你试过这样的吗?
ezetabs_controller.rb
def create
if params[:ezetab][:others] != nil
eze_params = ezetab_params.merge!(city: params[:ezetab][:others])
else
eze_params = ezetab_params
end
@ezetab = Ezetab.new(eze_params)
if @ezetab.save
redirect_to @ezetab
else
render 'new'
end
end
private
def ezetab_params
ep = params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
end