This is a simple question yet I’m having a hard time finding an answer. I have a select field in my form and I want to build the options array depending on my query records count.
I’m getting my count like so in my controller:
@num_pages= Page.where(:site_id => @siteid).count
I want my options array to just be like {[“1”, 1], [“2”, 2], …} all the way up to @num_pages.
Now I KNOW I can just do a loop and build that array but I want to know if there is a more “rails” way to do it. So that my select code could still be as simple as this:
<%= select_tag(:nav_order, options_for_select(@num_pages)) %>
…without using loops and array pushes and what have you.
UPDATE
Thanks to the responses my code now works like this. I decided to keep the mapping in the view instead of the controller however.
<% options_array = (1..@page_count).to_a.collect{|p| ["#{p}", p]} %>
<%= select_tag(:nav_order, options_for_select(options_array)) %>
Build the array in the controller?