Maxence
Maxence

Reputation: 2339

Formatting of drop down date selector

I am tweeking the rails getting started tutorial and try to add a drop down menu for each date element: day, month and year. My HTML is perfect yet no value is passed to the CREATE action of the controller

Here is my code

<p>
<%= f.label :start %><br>
<%= select_day(Date.today) %>
<%= select_month(Date.today) %>
<%= select_year(Date.today, :start_year => Date.today.year, :end_year => 5.years.from_now.year) %>
</p>

The field in the model is named start. I recon I dont pass the value to the controller but I have no clue how to do this...

Upvotes: 0

Views: 127

Answers (1)

Hoang Phan
Hoang Phan

Reputation: 2166

First, you'll need a name for each of your dropdown:

<%= select_day(Date.today, {}, name: 'start_day') %>
<%= select_month(Date.today, {}, name: 'start_month') %>
<%= select_year(Date.today, { :start_year => Date.today.year, :end_year => 5.years.from_now.year }, name: 'start_year' ) %>

Then you need to handle this in your controller:

class YourController < ApplicationController
  before_action :set_start, only: :create

  # your actions

  private

  def set_start
    params[:your_model][:start] = "#{params[:start_year]}/#{params[:start_month]}/#{params[:start_day]}"
  end      
end

Have fun!

Upvotes: 1

Related Questions