![](/img/trans.png)
[英]Getting multiple selected options with javascript from collection_select, RoR
[英]Rails 2.3.5 collection_select: how to grab attributes from selected object and send to javascript function
我有一個具有以下代碼的collection_select:
<%= collection_select(:address, :id, current_user.addresses, :id, :address1, options ={:prompt => "Select an address"},
html_options={:onchange => "insert_address_fields()"}) %>
當我在列表中選擇一個地址時,insert_address_fields會被正確調用,但是我要執行的操作是將所選地址的所有屬性(名字,姓氏,地址,城市,州等)發送給insert_address_fields函數。 諸如insert_address_fields(<% address.attributes %>)
,或者,如果需要的話,分別列出這些屬性。 我現在的設置是否可能做到而又不會使事情變得過於復雜?
collection_select生成一個帶有一組<options>
的<input>
,該<options>
從要設置的表的列中獲取。
您只能(並且應該)通過執行“ insert_address_fields(this.value)”將所選地址發送到insert_address_fields()。 在這種情況下,這將是html的輸入字段。
該功能應注意通過訪問數據庫從地址中獲取相關信息。
為什么?
否則,您將對視圖承擔過多責任。 視圖不應解析信息並將其發送給函數(反之亦然!)
如果您在地址屬性中添加一列,則需要更改視圖。 添加列應對您的應用程序視圖透明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.