select_year´s and select_month´s option selected by params

I searched for an opportunity to have the options of a selectbox selected that have been chosen by the user and sent by its form.

1. Checking if the form with the selectboxes was sent or not.
If it has not been sent, i use the current month and year. Otherwise the params['date'] is filled and can be used.

<% if params[:date] == nil
	@month = Time.new.month
	@year = Time.new.year
else
	@month = params[:date][:month]
	@year = params[:date][:year]
end %>

2. The selectboxes are generated and the selected option is the local variable (current date, or detected from params)
When using params we have a string that must be converted to an integer with to_i method.

<%= select_month(@month.to_i, {:add_month_numbers => true }) %>
<%= select_year(@year.to_i, {:start_year => 2006}) %>

Leave a Reply