Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass more than one value using Rails select_tag

I am using the rails form_tag helper, with the select_tag for a drop down within the form. For a given option in the drop down I need to be able to pass 3 values. Is it possible to do this using the select_tag?

The data that is being used to populate the drop down is formatted as an array of hashes, below is an example of the format of a given item in the array.

{ name: page['name'], page_id: page['id'], access_token: page['access_token'] }

So after the form is submitted I want to get each of those values in my controller as parameters.

The form using the select_tag

<%= form_tag '/facebook_credentials' do %>
  <%= select_tag(:option, options_for_select(session['facebook_pages'].collect { |x| [x[:name] ] }) ) %>
  <%= submit_tag 'Save', class: 'button expand' %>
<% end %>

By default the select_tag only allows for one value, is there a work around to this or a more efficient way to achieve the outcome I am looking for?

like image 443
Steve_D Avatar asked Dec 06 '25 21:12

Steve_D


1 Answers

You could find the "other values of the hash" by getting the hash which you just selected the name:

# view
select_tag(:option, options_for_select(session['facebook_pages'].collect { |x| [x[:name] ] })

# controller's action (where you submit your form)
selected_hash = session['facebook_pages'].find{ |x| x[:name] == params[:option] }

This code relies on the fact that each hash in the options of the select_tag has a uniq :name's value

like image 93
MrYoshiji Avatar answered Dec 09 '25 12:12

MrYoshiji



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!