New Discussion

Notifications

You’re not receiving notifications from this thread.

Why am I not getting any data inside the dropdown in browser?

2
Rails

My Controller has following code
@select = Store.find(current_user.store_id)
My View has:

<%= form_with(model:@store, local: true) do |f| %>
<%= f.select :id, options_for_select(@select.name, :id), {:include_blank => '--Select Item--'}, { :class => 'form-control' } %>
<%= f.submit 'Submit', :class => 'btn btn-sm btn-primary m-t-n-xs' %>
<% end %>

<%= debug(@select.name) %> This gives me the store name

Why am I not getting any data inside the Dropdown in browser?

Getting error

undefined method `map' for #Store:0x00007fd290f55ba8
Did you mean? tap

Hi Rajnish,

options_for_select expects an array or hash of options, see https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select

Your query - @select = Store.find(current_user.store_id) only returns a single object which won't work to populate a select field like you're wanting.

So just to clarify what you're after, can a User have multiple stores or only one? Your query suggests only one, but your desired result suggests multiple so I'm a little confused.

Jacks answer is spot on if a user can have multiple stores. If so, you should adjust your query to something like @select = current_user.stores if you have your associations setup correct.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.