简体   繁体   中英

How to pass selected value in select tag through form_tag rails

I need to pass selected option values in rails form tag.Below code I am using.When I click Add to User group button,it will call addToUserGroups action.I need to get selected dropdown value when I click this button.

               <%= form_tag(contoller: "custom_group", action: "addToUserGroups") do%>
                <%=submit_tag "Add to User Group", class: "btn mx-auto btn-primary",  style:   "width: 200px"%> 
              <% end %>

I need to send this selected dropdown value to addToUserGroups controller.How to achieve this?

<select id="select-update" class="form-control">
            
           
               <% @all_execgroup_list.each do |parameter| %>
               <option value="<%= parameter['groupname'] %>"><%= parameter['groupname']%></option>
             <% end %>
           </select>

You can use select_tag for that, like below

<%= select_tag:field_name_here, options_for_select(all_execgroup_list.all) %>

You can use the "select" option which is the second argument in the `options_for_select' helper.

<%= f.select :color, options_for_select(@color_options, selected: @color_value) %>

The important part here is selected: @color_value

Verbose version:

<%= f.select :color, options_for_select([["Blue", "blue"],["Green","Green"],["Yellow","yellow"]], selected: "yellow") %>

Yellow will be the default selected value in the dropdown.

options_for_select first argument takes a 2d array. What gets displayed in the dropdown is in first element in the array and the value is the second element in the array.

[["Display Name", "value"],["Display Name 2", "value 2"]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM