3

I am a beginner in rails and I have this array of times:

["08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30"]

How can I convert it into a dropdown using options_from_collection_for_select. I tried using

<%= select_tag "start_time", options_from_collection_for_select(@start_time,:id, :name ) %>

but it returned "undefined method 'name' for "08:30":String".

2 Answers 2

7

Assuming

@options = ["08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30"]

the code will be

<%= select_tag "start_time", options_for_select(@options) %>

See the options_for_select documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use like following

 <%= f.select :time, options_for_select(MONTHS, @job.time) %>

MONTHS => your array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.