我正在尝试调整现有的配置文件设置菜单,以便我可以预先确定某些字段,例如性别与下拉选项,而不是用户必须输入。 代码如下所示,
<%= form_for @profile, html: {multipart: true}, url: profile_path do |f| %>
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :firstname, required: true, placeholder: 'Firstname', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :lastname, required: true, placeholder: 'Lastname', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.date_field :dob, required: true, placeholder: 'Date of Birth', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :contactnr, required: true, placeholder: 'Contact Number', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :address1, required: true, placeholder: 'Address Line 1', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :address2, required: true, placeholder: 'Address Line 2', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :town, required: true, placeholder: 'Town', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :county, required: true, placeholder: 'County', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :country, required: true, placeholder: 'Country', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :postcode, required: true, placeholder: 'Postcode', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.text_field :gender, required: true, placeholder: 'Gender', class: 'form-control' %>
</div>
<form action="">
<input type="radio" name="sex" value="Male">Male<br>
<input type="radio" name="sex" value="Memale">Female
</form>
</div>
<div class="col-md-6">
<div class='uploaded-pic'>
<%= image_tag @profile.image.url(:medium) %>
</div>
<div class="form-group">
<%= f.file_field :image %>
</div>
<%= f.submit class: 'btn btn-success' %> </p>
</div>
<%end%>
具体来说,我想创建以下两段代码的某种混合,以便用户可以在下拉列表中选择一个值,然后在提交表单时将其保存。
<div class="form-group">
<%= f.text_field :gender, required: true, placeholder: 'Gender', class: 'form-control' %>
</div>
<form action="">
<input type="radio" name="sex" value="Male">Male<br>
<input type="radio" name="sex" value="Memale">Female
</form>
答案 0 :(得分:10)
您可以使用select
这样的
<%= f.select :gender, %w{Male Female}, :prompt => 'Select', required: true, placeholder: 'Gender', class: 'form-control' %>
有关详情,请参阅 API 。