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

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Rajnish Singh asked in 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?

Your syntax seems to be off. Try something like this:

f.select :id, options_from_collection_for_select(@select, :id, :name)

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

Want to stay up-to-date with Ruby on Rails?

Join 35,909+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.