Here is a code a *helper.rb. Instead of these 3 methods (they are working perfectly)
def years_of_birth_select(form)
form.select :year_of_birth, (1..31).to_a
end
def months_of_birth_select(form)
form.select :month_of_birth, months
end
def days_of_birth_select(form)
form.select :day_of_birth, years
end
I tried to call only one method
def date_of_birth_select(form)
form.select :day_of_birth, years
form.select :month_of_birth, months
form.select :year_of_birth, (1..31).to_a
end
and it was called as
= date_of_birth_select f
and it displayed only one select, :year_of_birth, select.
What did I do wrong and what should I do to be able to call date_of_birth_select correctly?
The form element that is displayed in the view is the return value of the helper method, which defaults to the last expression. In the first case, each method only has one line, so the result of
form.select ...is the return value, and so the form select is displayed properly. However when you merge them into one method, the return values from the first two lines are not returned, so you only get the:year_of_birthselect.To get all of them you have to concatenate the return values (strings) together:
The
html_safeat the end is to tell rails that it should not escape the string, which it will otherwise do by default.