How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"
All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.
This is a slight variation of his code:
In application.html.erb, or whatever layout file you’re using, put:
1 <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>
In application.js, or whatever JavaScript file you’re using, put:
1 $(document).ajaxSend(function(event, request, settings) { 2 if (typeof(window.AUTH_TOKEN) == "undefined") return; 3 settings.data = settings.data || ""; 4 settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN); 5 });
That’s all…
Opening a new window in the center of the screen with JavaScript
This JavaScript code will open a URL in a new window and center the window on the screen, instead of showing the window in a random place:
1 var url = 'http://google.com/'; 2 var width = 640; 3 var height = 480; 4 5 var left = (screen.width / 2) - (width / 2); 6 var top = (screen.height / 2) - (height / 2); 7 8 var xWindow = window.open(url, 'post', 'resizable=1,width=' + width + ',height=' + height + ', top=' + top + ', left=' + left); 9 10 xWindow.focus();
Combining CSS and JavaScript files with PHP to reduce page load time
This is a very simple utility class that I created which combines multiple files into one—in contrast to all other PHP scripts I’ve found elsewhere, which all work badly and are a million lines long, this one is simple to understand and maintain:
1 <?php 2 class Files 3 { 4 private static function process($files, $output, $base_dir = '') 5 { 6 $contents = ''; 7 8 # If this is a string then it's a search pattern 9 if(is_string($files)) 10 { 11 $files = glob($files); 12 } 13 14 foreach ($files as $file) 15 { 16 $contents .= file_get_contents($base_dir.$file) . "\n\n"; 17 } 18 19 # TODO locking 20 if ($fp = fopen($output, 'wb')) 21 { 22 fwrite($fp, $contents); 23 fclose($fp); 24 } 25 26 return $contents; 27 } 28 29 # 30 # Combine all CSS files in the css directory: 31 # 32 # echo Files::combine("css/*.css", "css/all.css"); 33 # 34 # Combine only specified files in the given order: 35 # 36 # echo Files::combine(array("css/core.css", "css/default.css"), "css/all.css"); 37 # 38 # 39 static function combine($files, $output_filename, $base_dir = '') 40 { 41 # Combine the files 42 if(!file_exists($output_filename)) 43 { 44 self::process($files, $output_filename, $base_dir); 45 } 46 } 47 } 48 ?>
The call to Files::combine should go in a file that is called on each request. On the first request to this file, the files are combined into one, on subsequent requests nothing is done, the only overhead is a call to file_exists.
Ideally the name of the output filename should contain a version number, so that an updated CSS file is not taken from the browser cache. This can easily be supported by reading the build number from somewhere and appending it to the output filename:
1 Files::combine("css/*.css", "css/all-v$version.css");
Now all you have to do is add this to your HTML :
1 <link rel="stylesheet" type="text/css" href="/css/all-v<?php echo $version ?>.css" media="screen" />
This also works for JavaScript files. Note that I don’t make an effort to minify anything as GZIP usually does a great job at minimizing bandwidth costs.
Disable javascript resizing of Firefox
There is absolutely not a single developer in the world who knows what size I like my browser, although many people think they do. To prevent resizing of FF make the following steps.
- Open browser preferences
- Choose “Content”
- Choose “Advanced” on the “Enable javascript” preference
- Untick “Move or resize existing windows”
Show and Hide (toggle) the Blueprint Grid with a JavaScript
This snippet-which could be improved on-allows you to easily display and hide the BlueprintCSS grid.
Put the following code inside the <head> element:
1 <style title="grid" type="text/css"> 2 .container { } 3 </style> 4 <script type="text/javascript"> 5 function toggleGrid() { 6 if (!document.styleSheets) return; 7 8 var cssTitle = "grid" 9 var selectorName = ".container"; 10 var cssText = "background: url('images/grid.png');"; 11 var css = new Array(); 12 13 // Find the stylesheet 14 for(i = 0; i < document.styleSheets.length; i++) 15 { 16 var styleSheet = document.styleSheets[i]; 17 18 if(styleSheet.title == cssTitle) { 19 if (document.styleSheets[i].cssRules) 20 css = document.styleSheets[i].cssRules; 21 else if (document.styleSheets[i].rules) 22 css = document.styleSheets[i].rules; 23 else return; 24 25 break; 26 } 27 } 28 29 // Find the selector 30 for (i = 0; i < css.length; i++) { 31 32 if ((css[i].selectorText.toLowerCase() == selectorName)) { 33 if(css[i].style.cssText == "") { 34 css[i].style.cssText = cssText; 35 } 36 else { 37 css[i].style.cssText = ""; 38 } 39 } 40 } 41 } 42 </script>
Change selectorName or cssText, if needed. Next add the following link, for example, to the footer:
1 <a href="javascript:toggleGrid();" id="toggle-grid">Show grid</a>
Now when you click the link, the grid will be displayed. Click again to hide it.
Note that this is a simple example of how to change a CSS style sheet dynamically with JavaScript. You can use it to change any selector at runtime.