SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Wednesday, June 25, 2008

remote_function

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


4 comments :

  1. THANKSSSSS FOR THE POST THAT'S BEEN VERY HELFUL

    ReplyDelete
  2. could 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

    ReplyDelete
  3. I think this is in prototype version can you do it for rails 3 with jquery. It might be helpful for everyone.
    thanks

    ReplyDelete
  4. Sure i will work this in jquery

    ReplyDelete