简体   繁体   中英

collection_select using formtastic_form

I am having two tables: company and department . In company table i have "departmentname" field for saving department name taken from department table. I am using formtastic form for collection select in my view.

<%= set.input :departmentname ,:as => :select, :collection => Department.all) %> 

i am getting department list, in my view. After saving "departmentname" field in Company Table is having department id and not name of the department.

How to save department name rather than id in my company table.

Thanks in advance.

what is name of the field in Department table whose value will be in departmentname in Company table? I guess it is name and your department table structure is like:

id    name
1     department 1
2     department 2

Try it as below:

:collection => Hash[Department.all.map{|d| [d.name,d.name]}]

however, I guess the following will work too (not sure though)

:collection => Hash[Department.all.map{|d| [d.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