Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding options_for_select with a 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>

That's the code that I have... can someone please explain what the current_publisher.earnings_statements.all.map{|s| [s.label, s.id.to_s]} bit is for?

I'm not a Rails expert and having a hard time understanding what it means. Any help would be greatly appreciated.

Thanks

like image 635
Shamoon Avatar asked Dec 13 '25 20:12

Shamoon


1 Answers

Map creates a new array based on what's returned by the block passed to it. In this case it returns an array of arrays which contains the label and id from earning_statements. It would look something like

[[label1, id1], [label2, id2]]

This array is then used as options for the select tag. The label being the text displayed and the id being the reference to which choice the user makes.

like image 175
Victor Solis Avatar answered Dec 16 '25 19:12

Victor Solis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!