Rails 3 offer application.html.erb as a layout template. However, whenever you send request to access a controller view, the content of application.html.erb will be loaded again. This seems not efficient, since header, navigation, footer only need to be loaded once.
In addition, when you need to have a javascript code executed in $(window).load for application.html.erb and another js method executed in $(window).load for <controller>.html.erb, this will mess up. I think the reason is that $(window).load can only execute once for each page.
So I wonder what's the best Layout practice for Rails 3.
Thanks
Rails 4 includes Turbolinks, which will only reload the body of your website when a link is clicked, instead of reloading all of the assets like javascript and CSS. If you want to fine tune what gets loaded further, you can take a look at pjax, but I think for most applications Turbolinks will be sufficient.
Related
Is it possible to render a react component in a rails partial through an AJAX call? Basically for performance reasons, I have to do some condition checks in order to render and initialize a react component in rails view. I cannot require the react JS upfront as well. I am thinking about rendering a partial through AJAX call and replacing the html in the view from jQuery. I am using react-rails for this purpose.
I'm not sure if this is the best way to do it. Other alternatives would be helpful too.
checked react-rails source code and had to manually call ReactRailsUJS.handleMount() via javascript.
For me, calling ReactRailsUJS.mountComponents() fixed my issue.
I see in my application.js file //= require turbolinks. I was wondering what turbolinks does in rails 5.0 because it is somehow getting in the way of my bootstrap buttons. Can someone please explain what turbolinks is and how i can fix my bootstrap problem?
Turbolinks is a gem that speeds up your app and makes it behave like a SPA (Single Page App), it does this by loading only the content between your body tags (using javascript) basically by making and AJAX request to the server, waiting the answer, deleting the old content and replacing it with the new content, handling the URL and browsing history.
For more info check https://github.com/turbolinks/turbolinks
Simple Explanation By Analagy:
Imagine you have a yellow page directory (a physical book directory). Every now and then, one or two phone numbers require updating. Rather than simply ordering an entirely new directory, you simply edit, within the directory itself, the phone numbers that need to be edited.
It's a LOT faster, and less expensive.
How do you know what numbers need to be edited? Well you'd make a phone call (AJAX request) and the yellow pages people will simply tell you that what certain few numbers need changing.
In other words, only the parts of the page which need changing will be changed with turbolinks. The problem with turbolinks is that it might not always be compatible with other javascript libraries out there.
I am developing an app using Ruby on Rails. In a specific view I have a textarea that uses Bootstrap Markdown.
The problem is that each time I visit that view, the textarea is rendered completely plain, without the Markdown functionality. Hitting F5 (refresh) renders the form correctly.
I tried to clear my browsers cache etc, but no luck. I always have to hit refresh to see the page correctly.
What may cause that?
Edit:
I am using this Markdown Plugin
I have also included the js files as stated in this question.
The turbolinks gem has been known to interfere with other javascript files and it's recommended that you remove it completely unless you really need it.
As Mohammad AbuShady said in his comment, you'll need to edit the part of the javascript files for your markdown that tells the page to start rendering. In your case this involves adding
$("#some-textarea").markdown({autofocus:false,savable:false})
inside page:load on the relevant pages.
In case you need turbolinks still, I found this very helpful: JQuery gets loaded only on page refresh in Rails 4 application
I had the same issue and adding jquery-turbolinks gem allowed the bootstrap-markdown editor to load perfectly for me.
I have a lot of html pages on my site that are static, but I serve them dynamically through the typical rails controller & view setup just because I want to use my application.html.erb layout and not have to worry about changing it in multiple places if I just cut and paste it into a static html file I put in my public directory.
I recently upgraded to Rails 3.2 however, and the asset pipeline and its precompiling of assets sounded like a perfect fit for this problem. However, I have not seen any good documentation on how to write a *.html.erb asset, and direct it to use application.html.erb, or any other layout for that matter. can someone post how to do this, or point me to a good resource? Thanks!
Instead of trying to serve the static html partials through the asset pipeline, have you thought about just caching them?
If what your worried about is actually getting the static partial to be rendered then you might want to try the following in your view. The following would be for your nav.
-cache 'main-nav' do
=render :partial => 'layouts/nav'
This way on the first load it would retrieve your partial, and all subsequent loads it would retrieve it from your cache, which if you have redis or memcache set up, should make it blazing fast.
I am trying to cache a partial which is rendered in a layout. This partial is computationally expensive so I only want to compute it once. It is not controller specific so the usual fragment caching doesn't seem to apply. I decided to use Rails.cache.fetch('menu') for the caching instead. Here is what the contents of the partial look like.
<% Rails.cache.fetch('menu') do %>
Partial code...
<% end %>
But when I do this it renders the partial twice. For some reason it stopped doing this in my development environment so I decided to try and deploy it. I am not so lucky with my production environment. The partial itself generates a menu that includes links to a lot of the records in the site to help improve navigation.
I originally tried putting the cache statement in the layout file but then it was rendering the layout twice.
I recently added a jQuery hack to remove the duplicate html so that it kinda "works" for now, but I would rather get it working properly. I don't want to go to the trouble of installing some complicated caching system such as Redis, that requires me to run another server program. That would be overkill for the task. There must be something in rails that is well suited to caching parts of layouts.
Should I try something completely different or is this a bug in rails? If it is a bug then is there a workaround I can use?
I figured it out. Instead of caching the partial in the view, I created a helper method that returns the rendered partial.
#Returns a menu for the application layout
def menu
Rails.cache.fetch('menu') { render :partial => 'all/menu' }
end
Then all I needed in the layout is this line
<% menu %>
Try clearing your production cache. Chances are you had a logic error that was causing it to render twice. In development, the cache is usually disabled so that's why when you fixed the problem it went away in development. If you have redeployed, the issue is probably gone from the production code as well, but if the cache has not been cleared, it will keep displaying the cached logic error.