Module ActionView::Helpers::PrototypeHelper
collection_select using remote_function
hi i want my collection_select(dropdown) to display the details of the person by selecting the person name using ajax in ruby on rails.Then i went for this remote_function helper.To more about the remote_function visit remote_function
First just have some knowledge of ajax & how it works
First in your html.erb file just have collection_select look like this
<div id="main" >
<b>PatientName</b><br/>
<%=collection_select 'patient','id',User.find(:all),:id,:firstname,{:prompt => 'Select the patient'},:class =>'select',
:onchange=>remote_function(:update=>'patient_details',:url => {:controller=>'patientfile',:action =>'get_details'}, :with => 'Form.Element.serialize(this)')%>
</div>
<div id="patient_details">
</div>
During onchange of collection_select its accessing the remote_function helper.The helper is calling the controller & action and the datas are getting updated in the div patient_details.
Controller sholud look like this
class PatientfileController < ApplicationController
def index
end
def get_details
@user=User.find(params[:patient][:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
end
Note here :with=>'Form.Element.serialize(this)' is used to get the person id which we are selecting.
Figure 1
Figure 2
THANKSSSSS FOR THE POST THAT'S BEEN VERY HELFUL
ReplyDeletecould you post your code for this. I am trying to do a very similar thing and it seems like a bunch of code is missing. thanks
ReplyDeleteI think this is in prototype version can you do it for rails 3 with jquery. It might be helpful for everyone.
ReplyDeletethanks
Sure i will work this in jquery
ReplyDelete