在 onchange 事件中将您更改的人员 ID 传递给 javascript 函数。
<%= select_tag "person", options_from_collection_for_select(Person.all, :id, :name, 1) ,
:onchange => "your_js_function_to_display_phone_for(this.value)"%>
触发 Ajax 请求并从数据库中返回电话号码。(如果人员列表很小,您也可以从客户端执行此操作)
<script type="text/javascript">
function your_js_function_to_display_phone_for(person_id)
{
// Given a person_id
// I am assuming this route will return me the Phone Number
new Ajax.Request('/person/get_phone_number/'+person_id,
{asynchronous:true, evalScripts:true,
method:'post',
onSuccess:function(request)
{
// Modify this place to whether update on particular div_id
// or do appropriate action with returned request.body
alert("Phone Number for this Person is: "+request.body);
},
parameters:Form.serialize(this)});
}
</script>
编辑:
你的控制器应该做这样的事情。
class PersonController
def get_phone_number
person = Person.find(params[:id])
phone_number = person.get_phone_number
render :text => phone_number
end
end