EastsideDev
EastsideDev

Reputation: 6639

Rails: Getting wrong selection in dropdown

Here's what I have in the view file:

<% users = {'John Doe' => 1, 'Jane Doe' => 2} %>
<% admins = [1,2,nil] %> 

<% i = 1 %>
<% admins.each do |user_id| %>
  <% org_struc_order = 'admin' + i.to_s %>
  <% org_struc_order_text =  'Admin ' + i.to_s%>
  <% if user_id %>
    <%= 'Current selection for ' + org_struc_order_text + ' is '+ users.key(user_id) %>
    <%= select( 'org_selections', org_struc_order, users, {:selected => users.key(user_id)}, { :include_blank => true } ) %>
  <% else %>
    <%= 'Select ' + org_struc_order_text + ' from dropdown' %>
    <%= select( 'org_selections', org_struc_order, users, { :include_blank => true } ) %>
  <% end %>
  <% i = i + 1 %>
<% end %>

This is partially working. When the form is displayed, the same value "John Doe" is displaying in the first and second select dropdown boxes, but the caption above it is correct. Above the first box, it says:

Current selection for Admin 1 is John Doe

and the select box, I have:

John Doe

Above the second box, it says:

Current selection for Admin 2 is Jane Doe

but in the select box, I see:

John Doe 

What's going on?

Upvotes: 0

Views: 187

Answers (1)

Arun
Arun

Reputation: 885

Problem with this line I think,

<%= select( 'org_selections', org_struc_order, users, {:selected => users.key(user_id)}, { :include_blank => true } ) %>

Corrected one:

<%= select( 'org_selections', org_struc_order, users, {:selected => user_id}, { :include_blank => true } ) %>

You are passing user name Instead of user_id for selected value.

Upvotes: 2

Related Questions