Ruby on rails 3: How to pull contents of another page into existing page without page refreshing
I have a list of links on a page:
PAGE 1
Link.1
Link.2
Link.3
Link.4
Etc.
I want to make it possible for a user to click a link and have the contents of the page for the link show up on the same page with out the page refreshing. I would basically pull the contents into a div on the page. When ever another开发者_如何学编程 link is clicked I would like for the present content to disappear making room for the new content.
This kind of thing is NEW to me. I would appreciate if anyone could point me towards a tutorial which clearly shows how to do this.
Thanks in advance
This should help: http://keithschacht.com/getting-ajax-to-work-in-rails-3-with-jquery/
If you want an effect similar to github, maybe pjax is what you're looking for: http://pjax.heroku.com
It makes it VERY easy to replace the content of a site just leaving out your application layout. Used by github and basecamp afaik. It also degrades nicely :)
UPDATE:
Pjax works like this:
- It looks if the browser supports html5 history api.
- If not, it just sets the window location and everything is as usual
- If so, it will send ajax requests to the server, equipped with a special header.
- The server (in case of rails) can read the header and will send the response without the layout (which would be render :layout => false in Rails)
Even though it's not a completely out of the box solution yet, it's very clean and simple. :)
Maybe this two further links will be useful for you:
https://github.com/defunkt/jquery-pjax
https://github.com/rails/pjax_rails (note that this is for Rails 3.1+)
If you are using Rails >= 3.2, you can also give a try to the rails-ajax gem.
All you have to do is configure it with your main default container, and your website will be automatically ajaxified using this container. No need to alter your application's code.
It handles a lot of hassle for you: bookmarking, history, scripts executions, redirections, forms, fallback for JS-disabled browsers...
精彩评论