ruby on rails - Show the last month as default in a dropdown list -
i have dropdown list months. how can show last month default? example: month september, i'd set default month august.
my view:
the dropdown list
<% @date = date.today @months = [] (0..11).each |m| @months << [@date.next_month(m).strftime("%b"), @date.next_month(m)] end %> <div class="col-md-8"> <%= f.select :datepay, options_for_select(@months), { :required => true, :onchange => "alert()" } %> </div>
currently, @months
array not contain previous month.
so, include previous month in collection follows:
@date = date.today @months = [] (-1..11).each |m| #mark here starting range -1 i.e august, 2016 @months << [@date.next_month(m).strftime("%b"),@date.next_month(m)] end
like have used next_month
next month, prev_month
can used previous month.
<%= f.select :datepay, options_for_select(@months, @date.prev_month), { :required => true, :onchange => "alert()" } %>
Comments
Post a Comment