简体   繁体   中英

Populating a select box in rails with names from a model

I've been trawling through stack as well as the rest of the internet for an answer to this question and nothing really seems to provide "enough" of an answer to make any headway. I'm a complete newbie to web development so any help would be appreciated.

In my models each user has many roles through assignments, and roles have many users through assignments.

When I'm creating new assignments I want to have dropdown box with the first+last name of each user. I can't seem to find anything on the internet to guide me on this but then again maybe I'm not using the right terminology to search for stuff. Any pointers would be very much appreciated!

I've tried:

<%=f.select :user_id, select("assignment", "user_id", 
  User.all.collect {|u| [ [u.name,u.surname].join(" "), u.id ] },
  { :include_blank => true }) %>

but all that returns is a blank selectbox. I've run it through the collect query through the console to ensure that it's working but for some reason it's not populating the select box.

select("assignment", "person_id", 
  Person.all.collect {|p| [ [p.first_name,p.last_name].join(" "), p.id ] },
  { :include_blank => true })

or define full_name in your model and use

options_from_collection_for_select(@people, 'id', 'full_name')

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