My Ajax call is almost working, but i can’t figure why my layout is still rendered? here is the context:
I have a contact page on my site that use the linkedIn API. I want to load the LinkedIn info from an ajax call when my page is loaded. This work fine.
But the result of my .load() is rendered with the layout of my site. I only want to render the html in my ajax_contact.html.erb..
contact.js
$(document).ready(function() {
$(".info_perso").load( '/contact/ajax_contact', {name:"benoit"}, function(data){ $(".info_perso").html( data );})
});
ajax_contact.html.erb:
<%if !@profile.nil?%>
<%=image_tag(@profile.picture_url)%><br />
<%=@profile.first_name %> <%=@profile.last_name %><br>
<%=@profile.headline%><br>
<%=@profile.industry%><br> <br />
<%=@profile.summary%><br>
<label class="linkedin_title">Education</label><br>
<% @profile.educations.all.each {|edu| %>
Degre : <%=edu.degree%><br>
Années : <%=edu.start_date.year%> - <%=edu.end_date.year%><br>
Concentration : <%=edu.field_of_study%><br>
Universite : <%=edu.school_name%><br>
<%}%>
<label class="linkedin_title">Experience</label><br>
<% @profile.positions.all.each {|pos| %>
<label class="linkedin_subtitle"> Company:</label> <%=pos.company.name%><br><br>
<label class="linkedin_subtitle">Titre :</label> <%=pos.title%><br>
<label class="linkedin_subtitle">Années :</label> <%=pos.start_date.month%>/<%=pos.start_date.year%> - <%= pos.end_date? ? pos.end_date.month.to_s + "/" + pos.end_date.year.to_s : "now"%><br>
<label class="linkedin_subtitle">Description :</label> <%=pos.summary%><br/><br/>
<%}%>
<%else%>
<%=link_to "Activer linkedIn", :controller=>"linkedinAuth", :action=>"benoit", :name=>"benoit", :callback=>"/linkedinauth/callbackbenoit"%>
<%end%>
contact_controller.rb:
def ajax_contact
name = params[:name]
linkedinInfo = LinkedinApiInfo.find(1)
linkedinCred = LinkedinCredential.find_by_name(name)
if !linkedinCred.nil?
client = LinkedIn::Client.new(linkedinInfo.apiKey, linkedinInfo.secretKey)
client.authorize_from_access(linkedinCred.acctoken, linkedinCred.accsecret)
# Pick some fields
fields = ['first-name', 'last-name', 'headline', 'industry', 'num-connections','educations', 'num-recommenders','recommendations-received', 'summary', 'positions','picture-url']
@profile = client.profile :fields => fields
@profile.recommendations_received.all.each {|rec| puts rec.recommendation_text}
puts @profile
end
rescue SocketError
puts "Unable to connect"
render "ajax_content", :content_type=>"text/html", :layout=>false
end
Even with the :layout=>false, my layout is still showned!!! anyone can see the problem?
add
render ‘ajax_contact’, :layout=>false
to your action, at the end, also partial views name should start with _, so your view should be named ‘_ajax_contact’