R – How to fill a page in Div in RoR

ajaxrubyruby-on-rails

I have a div called "main", and I have two hyper link, one is called "Google", another is "Yahoo!" , I want the user click the "Google", and the google.com will fill in the main div within refresh, same as "Yahoo!". what should I do in RoR?

Best Answer

You cannot load the content of google or yahoo directly to a div via AJAX as the browser security model prevents XmlHttpRequests to other domains, so you basically have 2 options:

  1. Use an iFrame to show the Google or Yahoo page
  2. Load the requested page on the server and populate the div with the loaded html.

Since for 1.) you don't need any server side code, I guess you are looking for a way to do 2.) in RoR. You can use the httpclient library for that, but be aware that including foreign html in your page is a mess and will probably not work as expected without great effort.

If you are just trying to show google search results in you app, you should have a look at their api.