Saturday, 28 April 2012

Fixing Hibernate Problems in Linux Mint

Hibernate was working fairly inconsistently on my machine; sometimes it would work, and sometimes it would hang with s2disk pending onscreen. Google turns up a lot of Linux hibernate problems, below is how I resolved mine.

First, ensure you have more swap space than RAM (1.5 - 2x RAM as a guideline), using the command:
 swapon -s  

If you don't have enough space, follow the instructions here before continuing.

Once you are sure you have enough swap space, if hibernate still isn't working, install the powermanagement-interface package, using:
 sudo apt-get install powermanagement-interface  

This got hibernation working on my machine, I hope it helps you too.

Update: Make sure you check out Part II of this post, should this post not help you fully resolve your hibernate problems.

Friday, 27 April 2012

Finding and killing a process in Linux

To find a process, use
 ps ux | grep <process_name>  

Where <process_name> is the name, or part of the name of the process you want to find.

Look up the process ID (the number in the output), and then use
 kill -9 <process_number>  

Where <process_number> is the process ID of the process you want to kill.

Thursday, 26 April 2012

Increasing swap file size in Linux

If you need to increase your Linux swap space, but don't want to repartition your hard drives, you can use a swap file.

First, create an empty file, of the size you want.
 dd if=/dev/zero of=/mnt/my_swap bs=1M count=2048  

The above creates an empty file of size 2GB, called mnt/my_swap. To create a file of a different size, replace the 2048 with the size of swap file you require, in MB.

Next, format the file as swap space:
 mkswap /mnt/my_swap  

Then tell the operating system to begin using it:
 swapon /mnt/my_swap  

Finally, to persist the change, and ensure the OS uses the new swap file on every boot, open /etc/fstab and add the following line:
 /mnt/my_swap none swap sw 0 0  

To open /etc/fstab, you can use gedit with this line:
 gedit /etc/fstab  


Additionally, to check the current swap spaces in use by the system, you can use:
 swapon -s  


With thanks to DistroWatch Weekly, Issue 388, 17 January 2011 for this information!

Wednesday, 25 April 2012

attr_accessor and attr_accessible in Rails

While trying to figure out why I was getting this error in a Rails application:
 WARNING: Can't mass-assign protected attributes  

I realised I'd confused the two below terms in my Rails model:

 attr_accessor  
is built into Ruby and defines a getter and setter method for a class attribute.


 attr_accessible  
is defined by Rails and makes the attribute mass-assignable.

Generally, in a Rails application, it's attr_accessible we want to be using.

Monday, 23 April 2012

Creating a Redcar Launcher in Linux

The inspiration for the Redcar editors name

To create a Redcar launcher in Linux, first create a script which launches Redcar from the command line (I keep my scripts in ~/bin). Save this as start_redcar.sh, or similar (the .sh extension is required). The contents of this script are:
 #!/bin/bash
 source ~/.bashrc  
 /home/<USERNAME>/.rvm/gems/ruby-1.9.3-p125/bin/redcar  
Redcar Launcher Script

In the above script, <USERNAME> is my Linux username, and it's followed by my Redcar installation path. Your Redcar installation path may be different - to find it, open a terminal window and type:
 which redcar  

This will output your Redcar installation path.

The key to the above script is the source command. This means the launcher will have access to your RVM and Redcar installations - without this line, the script will work when you run it from within a terminal window, but fail when you create the launcher, as the terminal has access to .bashrc, whereas by default, the launcher won't.

To test the script, first make it executable, by opening a terminal window and navigating to the script directory. Then run
 chmod +x start_redcar.sh  

Then launch the script, typing
 ./ start_redcar.sh  

Where start_redcar.sh is the name of your script.

If Redcar opens, everything has gone according to plan so far - now we need to create the launcher. To do this, right click on the desktop, and find Create New Launcher. Under Name, enter "Redcar" (without the quotes), and under Command, browse to the script we previously created. Finally, to give the launcher the Redcar icon, click the icon, and browse to:
 /home/<USERNAME>/.rvm/gems/ruby-1.9.3-p125/gems/redcar-icons-0.3/icons/redcar-icon-beta.png  

Press OK and the launcher will be created on your desktop. To add this to Cairo-dock, or another Linux dock interface, simply drag the launcher onto the dock.

Examples of the two files created above can be found in this gist. Enjoy your new Redcar launcher!

P.S!  Are you learning Ruby or Ruby on Rails? I highly recommend The Ruby Programming Language and The Rails 3 Way as useful references. Of course, you can always check out the bookstore for other books I've found useful.

Update May-20-12: This post is now linked to from the Redcar Wiki!


Friday, 20 April 2012

Undoing a Rails generate

To undo an action in Rails (generation of a scaffold, model, migration, etc), you can use
 rails destroy  

For example, generating and then destroying a controller called Posts would look like this:

 $ rails generate controller Posts  
    create app/controllers/posts_controller.rb  
    invoke erb  
    create  app/views/posts  
    invoke test_unit  
    create  test/functional/posts_controller_test.rb  
    invoke helper  
    create  app/helpers/posts_helper.rb  
    invoke  test_unit  
    create   test/unit/helpers/posts_helper_test.rb  
    invoke assets  
    invoke  coffee  
    create   app/assets/javascripts/posts.js.coffee  
    invoke  scss  
    create   app/assets/stylesheets/posts.css.scss  

$ rails destroy controller Posts  
    remove app/controllers/posts_controller.rb  
    invoke erb  
    remove  app/views/posts  
    invoke test_unit  
    remove  test/functional/posts_controller_test.rb  
    invoke helper  
    remove  app/helpers/posts_helper.rb  
    invoke  test_unit  
    remove   test/unit/helpers/posts_helper_test.rb  
    invoke assets  
    invoke  coffee  
    remove   app/assets/javascripts/posts.js.coffee  
    invoke  scss  
    remove   app/assets/stylesheets/posts.css.scss  

Thursday, 19 April 2012

Adding a background image to tabs menu in Blogger


Add the following CSS to Template -> Layout -> Advanced -> Add CSS

 #PageList1 ul {  
 background-image: url(<url_to_image>);  
 }  

Where <url_to_image> is the URL to the image you wish to use.

P.S! Are you looking to improve your blog? I recommend you check out Bloggers Boot Camp  and Google Blogger For Dummies. Of course, you can also browse the bookstore for other inspiration!

Wednesday, 18 April 2012

Sizing a background image for Blogger header

To size a background image for your Blogger header; the image width needs to be the width of your blog (check in Template -> Customise -> Adjust Widths), and the image height needs to be 200px.

Go to Layout, and edit the header widget (the one with the title text on it). This will allow you to upload your image.

Tuesday, 17 April 2012

Removing the navbar on Blogger

I've been restyling this blog lately (as I'm sure regular visitors have noticed!), and picked up this little useful information today:

To remove the navbar on a Blogger site, first backup your template (go to Template -> Backup/Restore), then go to Template -> Layout -> Advanced -> Add CSS. Paste the code below into the CSS box and the navbar will no longer appear.


 #navbar-iframe {  
 display: none !important;  
 }  




This is the the claim token for Technorati and will be removed soon: A8KTUYQ2C8ET

Monday, 16 April 2012

Conditionally hiding the sidebar on Blogger

I've recently added a small bookstore to this blog, as I thought my readers might be interested in the books I find interesting and useful. You can access it via the link in the bar above this post. If you open the "Bookstore" tab above, you'll notice that the right sidebar isn't present there, yet reappears when you reopen the "Blog" tab. This is achieved by a inserting a bit of CSS magic at the top of the HTML of the Bookstore page:

 <style type="text/css">   
  .main-inner .columns {padding-right: 0;}  
  .main-inner .fauxcolumn-center-outer {right: 0;   
  _width: expression(this.parentNode.offsetWidth -   
  parseInt("$(main.column.left.width)") + 'px');}  
  .main-inner .fauxcolumn-right-outer {width: 0px;}   
  .main-inner .column-right-outer {width: 0px; margin-right: 0;  
  display: none;}  
 </style>   

Credit to the greene teapot blog for this little gem!

Wednesday, 4 April 2012

Error installing pg gem in Linux

If installing the pg gem in Linux fails with this error:

checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...
 An error occured while installing pg (0.13.2), and Bundler cannot continue.


Installing libpq-dev should solve the problem.
 sudo apt-get install libpq-dev  

Tuesday, 3 April 2012

Searching Flickr using the Flickraw gem

The more I look at Ruby, the more I appreciate just how powerful and succinct  it is. I'm sure my current Ruby coding style isn't yet close to The Ruby Way,but I'll keep working on that. In the meantime, I wrote this script over lunch, in preparation for a Rails application I have in mind. The script searches flickr for photos tagged with a word you supply as a command line argument, then picks a random one and launches the browser to display it. You'll need both the Flickraw and Launchy gem installed for this script to run. You'll also need a flickr API key, which you can apply for here.


Install required gems:
 gem install flickraw  
 gem install launchy  
The code:
 #!/usr/bin/env ruby  
 require 'flickraw'  
 require 'launchy'  
 FlickRaw.api_key    = 'YOUR_API_KEY_HERE'  
 FlickRaw.shared_secret = 'YOUR_SHARED_SECRET_HERE'  
 results = flickr.photos.search(:tags => ARGV[0])  
 rand = Random.new.rand(results.length)  
 info = flickr.photos.getInfo(:photo_id => results[rand].id)  
 url = FlickRaw.url_b(info)  
 Launchy.open(url)  
 (replace YOUR_API_KEY_HERE and  YOUR_SHARED_SECRET_HERE with your information from flickr)


Save this as flickr_single_tag.rb, and then run it, supplying a single search tag on the command line:
 ruby flickr_single_tag.rb surf  


Last time I ran it (with surf, as above), I got the image below. It's kind of fun to see what kind of images the returns! 





This gist contains the code from this post.