I have my options in an array
开发者_JAVA技巧@groupings = [["Text 1", "value_1"],["Text 2", "value_2"]]
so im using a
select_tag(:groupby, options_for_select(@groupings, params[:groupby]))
I'd like to populate an H3 tag on the page with the selected text. (ie "Text 2" if params[:groupby ] = "value_2" )
Is there a rails way to do this? Other than manually deriving it from the @groupings array?
TIA, Kirby
Maybe all you need is a simple find
call, and these are often best wrapped in a helper:
def label_for_grouping(group_id)
grouping = @groupings.find { |g| g[1] == group_id }
grouping and grouping[0]
end
Then later:
<h3><%= label_for_grouping(params[:groupby]) %></h3>
This look-up would be easier if you had a Hash version of your @groupings
variable, or perhaps simply kept it as a Hash and rendered it as an Array using .to_a
where required.
精彩评论