runderscorer
runderscorer

Reputation: 135

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.

Upvotes: 3

Views: 160

Answers (2)

amb110395
amb110395

Reputation: 1545

Pass the selected option like this:

:selected => params[:default_value]

You can pass a parameter, string, etc whatever your case may be.

Upvotes: 0

varatis
varatis

Reputation: 14740

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.

Upvotes: 2

Related Questions