Nick Vanderbilt
Nick Vanderbilt

Reputation: 2535

How to display month and year selection for credit card

I am using Rails 3.1 . Here is the code I have which asks user to enter credit card expiration month and year.

  <div class="field">
    <%= f.label :expires_on, 'Expiration date' %>
    <br />
    <%= select_month nil, {add_month_numbers: true}, {name: 'creditcard[month]', id: "card_month"} %>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: 'creditcard[year]', id: "card_year"} %>
  </div>

Above code works. However the issue is that if there was a validation error then the selected expiration and month are reset.

I tried f.select_month but that is not supported.

Upvotes: 6

Views: 4167

Answers (1)

Pragnesh Vaghela
Pragnesh Vaghela

Reputation: 1317

Try something like this:

<p>
  <%= f.label :expires_on, 'Expiration date' %><br />
  <%= f.date_select :expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :use_month_numbers => true %>
</p>

Upvotes: 15

Related Questions