简体   繁体   中英

Rails: How do I make select send an object instead of a string?

I have a drop down menu of users, when I select one and submit my form the controller receives the user object as a string rather than the object itself. How do I make a menu whereby the user object itself is submitted?

<%= form.select :recipient, @users %>

You want to use collection_select instead of select .

Select is for rendering information from a drop down menu it is not meant for dynamic database information. Collection_select on the other hand is meant to pull things from a database, it allows you more options such as collection_select :label, :collection, :server information, :display information

Do this instead:

<%= form.collection_select :recipient, @users, :id, :email %>

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