Understanding options_for_select with map in Rails
<form id="earnings_select" action="<%= publishers_earnings_statements_url %>" method="get">
<% unless current_publisher.earnings_statements.all.size == 0 %>
<%= select_tag 'id',
options_for_select(current_publisher.earnings_statements.all.map{|s| [s.label, s.id.to_s]}, params[:id]),
:id => 'earnings_select_id' %>
<input type="submit" value="Go">
<% end %>
</form>
What is the code I have ... can someone explain what the bit is for current_publisher.earnings_statements.all.map{|s| [s.label, s.id.to_s]}
?
I'm not a Rails expert and don't understand what that means. Any help would be greatly appreciated.
thank
+3
source to share
1 answer
The map creates a new array based on what is returned by the block passed to it. In this case, it returns an array of arrays that contains the label and ID from earning_statements. It would look like
[[label1, id1], [label2, id2]]
This array is then used as an option for the select tag. The label is the display text, and the ID is a link to the choice the user makes.
+5
source to share