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?

Reply

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

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

Getting error

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

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.