<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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With