用户有两个选项:“每月平均值”或“一次性实例”。在_form中选择一个是否有一种方法可以根据用户的选择使用javascript或其他东西调整_form中f.date_select的格式?
选择“每月平均值”后,我希望用户只能看到并选择:月份和年份。选择“一次性实例”后,我希望用户查看并选择:日,月,年。
_form.html.erb
<%= form_for(@quantified) do |f| %>
<% if @quantified.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@quantified.errors.count, "error") %> prohibited this quantified from being saved:</h2>
<ul>
<% @quantified.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="america">
<form>
<%= f.select :categories, Quantified::CATEGORIES %>
<br>
<br>
<div class="form-group">
<%= f.text_field :name, class: 'form-control', placeholder: 'Enter Name' %>
</div>
<div class="form-group">
<%= f.text_field :result, class: 'form-control', placeholder: 'Enter Result' %>
</div>
<div class="form-group">
<%= f.text_field :metric, class: 'form-control', placeholder: 'Enter Metric' %>
</div>
<div class="date-group">
<label> Date: </label>
<%= f.date_select :date, :order => [:month, :day, :year], class: 'date-select' %>
</div>
<div class="america2">
<%= button_tag(type: 'submit', class: "btn") do %>
<span class="glyphicon glyphicon-plus"></span>
<% end %>
<%= link_to quantifieds_path, class: 'btn' do %>
<span class="glyphicon glyphicon-chevron-left"></span>
<% end %>
<%= link_to @quantified, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
</div>
</form>
</div>
<% end %>
quantified.rb
class Quantified < ActiveRecord::Base
belongs_to :user
scope :averaged, -> { where(categories: 'Monthly Average') }
scope :instance, -> { where(categories: 'One-Time Instance') }
CATEGORIES = ['Monthly Average', 'One-Time Instance']
end
提前感谢您的帮助!