开发者

Ruby on Rails ajax - dynamically loading all pages

开发者 https://www.devze.com 2022-12-19 06:41 出处:网络
I have a flash media player (similar to lala.com) that needs to continue to stream while people click around. I don\'t want to use an iframe.

I have a flash media player (similar to lala.com) that needs to continue to stream while people click around. I don't want to use an iframe.

So, I need to dynamically load all site pages with ajax no matter what link people click on.

I've got this working with Rails and JQuery for a single page. With this method I have to place a file.j开发者_如何学运维s.erb file for whatever controller is called. Example: example.com/home is called and I have to have an index.js.erb in the views home dir to respond to this.

I used: http://railscasts.com/episodes/174-pagination-with-ajax

to get this to work on one page, but it wouldn't be DRY at all to copy .js.erb files to every controller.

Is there something I can do with the main application_controller or even with routes.rb?


I found a way to do this. You can load whatever pages you want via the jquery load method.

$('#result').load('ajax/test.html #container');

They will then be placed in the div with the #result id. You can also specify a page fragment with #container so you only load the part you want. This allows all your controllers to stay in tact etc with no extra js.


This is really going to impact your visibility to search engines. I also think it will mean you need to think about the flow of your controllers. Rather than having controllers responding with views that represent a page, controllers will respond with snippets of HTML/JSON/etc that get injected into the home page. Your home page acts as a coordinator, loading the content in and out. Perfectly fine to have js.erb files in each view folder ... as they should be returning content specific to an individual controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消