Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the default value in my form on page load? (Rails)

I'm trying to set the default value of my form field, but I'm not sure where I can pass in that information. Here's what I have so far:

def month_options
  [["JAN", "01"], ["FEB", "02"], ["MAR", "03"], ["APR", "04"], ["MAY", "05"], ["JUN", "06"], ["JUL", "07"], ["AUG", "08"], ["SEP", "09"], ["OCT", "10"], ["NOV", "11"], ["DEC", "12"]]
end

In my view, I'm using this:

<%= select_tag :month, options_for_select(month_options, params[:month]) %>

I'd like to be able to set the default value of my form field to the current month.

like image 491
runderscorer Avatar asked Dec 06 '25 05:12

runderscorer


1 Answers

Perhaps

<%= select_tag :month, options_for_select(month_options, params[:month] || "JAN") %>

...if you want the selected value to default to "Jan" of params[:month] is nil.

like image 134
varatis Avatar answered Dec 07 '25 22:12

varatis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!