Friday, November 4, 2011

Getting delayed_job to Work with Rbenv and Mongoid

Rbenv is another option that we can use besides RVM to be able to manage different Ruby versions.  I have always struggled getting RVM to manage everything right, from gemsets to bundles. Since Bundler manages your gems and dependencies for you, gemsets are just an extra layer that caused me a bunch of problems.  So I switched to Rbenv for my production environment.  All went well except delayed_job. It would work just fine with rake jobs:work, but script/delayed_job start would start just fine, but wouldn't pull anything from the database. I am using Mongoid, so I assumed that the script was defaulting to the sqlite database.


Well, I looked at the script and noticed that their was a shebang at the top mentioning ruby (#!/usr/bin/env ruby). I just changed it from ruby to ruby-local-exec and it started working again! This made the script run in the bundled environment and pick up on that fact that I am using Mongoid instead of sqlite. Hope this helps someone.

Tuesday, October 25, 2011

Another Rails Editor and User Authentication

Just another update for any of my past students following along.  Redcar seems to have won over all of the students as their favorite Rails editor.  To install it, you just type "gem install redcar".  It looks pretty promising.  The students found it on their own, and preferred it to Sublime Text Edit, mostly because it's free and doesn't ask you to pay for it every 10 minutes.

Also, we have been working on developing authentication systems, and the following Railscasts.com videos are videos that I have been using for the class.

http://railscasts.com/episodes/270-authentication-in-rails-3-1

and

http://railscasts.com/episodes/274-remember-me-reset-password

Migrating from Wordpress to Blogger

Here are some detailed instructions on how I moved my blog from Wordpress to Blogger, including how to get my old Wordpress links to redirect to my new blogger style links.  I would like to thank Jesse Stay for his blog post that convinced me to switch and gave me a head start in the process.  After the switch, I also learned that Google+ will integrate with Blogger, so that is even more of an incentive.

This should work with a self-hosted Wordpress blog and a Wordpress.com blog.  If you don't have your own domain, you can transfer your blog to Blogger, but you will not be able to preserve and redirect your old links.

Before you begin, you might want to use OpenDNS for your DNS on your local machine. You can refresh the DNS cache at any time, so you can see your changes immediately instead of waiting up to 2 days for the DNS cache to expire. Plus using OpenDNS has many benefits. Instruction for configuring your local machine to use OpenDNS can be found here.

Also, if you want to redirect your old Wordpress style URL's to your new Blogger style URL's you will need a server somewhere that will handle the redirections.  If you don't have one, you can use Heroku for free.  I will show you how to set it up, but you will need Git, and Ruby installed on your machine.  Mac users already have ruby installed (they might need to install x-code from the app-store), and they can get git from here.  I also recommend installing RVM to install Ruby version 1.9.2. Window's users can get Ruby and Git (and a lot of extras) from railsinstaller.org. You only need Ruby, Git, and Bundler, so you don't have to install all of the packages. I don't have a Windows machine to try it out, so this is just my best guess.

We will be doing the following steps to make the transfer from Wordpress to Blogger:
  1. Exporting the posts and data out of Wordpress and importing them into Blogger.
  2. Configure Blogger to meet our needs.
  3. Set up a redirection server that will redirect Wordpress style links to Blogger style links.
  4. Transfer the domain from Wordpress to Blogger.
  5. Install Disqus comments (if you want to use it) on Blogger and migrate all of the discussions.
I organized the steps to reduce the amount of downtime your blog will face. You should be able to keep the blog up the full time, but your readers might not be able to comment until the DNS changes have fully propagated. Also, you will lose all of your Facebook likes and +1's, except on your main domain.  Facebook/Google will not follow your redirections and combine the old Likes/+1's with your new ones.  If this is unacceptable, then you will need to stay with Wordpress.

Exporting your posts from Wordpress and importing them into Blogger.
To export your Wordpress post and import them into Blogger just follow these instructions. My blog was less than 1MB, but if you have a bigger blog, you will have to download the converters and run them manually. More details on that process can be found here.

You should create a new Blogger blog, and don't worry about the blogspot.com URL if you have your own domain. Once it is up you can change the URL to the right domain. After you import your Blogger file, you should see all of your posts and comments. They will all be drafts, and you can select them all and publish them.

There are a few things that might be a lot of work after you complete this step. First of all, all the pictures will work, but only until you move your domain to Blogger. They are pointing to your Wordpress blog, so once your domain is moved over, they will stop working. You will need to get them off of your old blog and upload them to Blogger, or somehow host them on your redirection server. I didn't have many picutres, so I just uploaded them to Blogger, but I will describe how you can host them on the redirection server. Also, on Blogger, your urls will be missing words like "the", "a", and "with". Which will create a little bit more work down the road, but I'm not sure if you can do anything about that.

Configure Blogger
You can look at all of the settings.  I wouldn't set your domain until after you have the redirections in place. You can also take the time to edit the layout and add widgets to the side.  Make sure your happy with how it looks, so you will know if you want to continue or not. Your Wordpress blog should still be up. You can also add your Facebook like buttons I described in the previous post.  Don't install Disqus until after you have your redirections in place either.

Now is also a good time to follow these instructions to get your author picture in your search results.  You can also see Google's instructions as well.  What I did was create an About page and linked to it with a text widget (not the page's widget) that is included on all pages in my blog. I edited the HTML to include the a link with rel="author" in it. On the About page, I have a picture, short bio, and a link to my Google+ profile with rel="me". Make sure the Google+ link begins or ends with a +, or is a profile button, with rel="me" in the anchor tag (not rel="author").

Set up your redirections
To redirect your Wordpress style URL's to your new Blogger blog, you will need a server to handle the redirections. If you have an Apache server, you can use the rewrite mod to create the redirections. Below is a sample .htaccess file that I used to redirect my links.  You will need to customize it for your domain.  The redirection server needs to be a sub-domain of your domain. So if you host your blog at www.example.com or blog.example.com, your redirection server needs to be something.example.com (I used old.christopherslade.com).

Here is the .htaccess file I used before I thought about using Heroku.

The .htaccess file includes redirections for my author page, about page, categories, tags, the feed, and the posts.  In Blogger, there are just labels, so both categories and tags redirect to the label pages. I also had to add in some rules to redirect posts where Blogger didn't include words like "the" and "a" in the URL.  You will need to search for these and add in those redirections as well.

If you don't have a server to host the redirections (I migrated to ditch the server), you can use Heroku for free.  When I was trying out different blog solutions, I ran across toto and learned a little about Rack.  Since I develop in Rails, I really wanted to make toto work, but I couldn't easily migrate my posts. But I did learn enough to know that I could use Rack to redirect URL's. I created a simple Rack app that would redirect the URL's, and even host some static assets. You can use this app to redirect your URL's and even host your pictures from Wordpress.

I uploaded my app onto Github.  If you want to use it, just clone it and change the config.ru file. Again, you will need Git, Ruby, and Bundler installed on your machine. You can then use Heroku to host your redirection server.  Here is my config.ru file that you will need to change:
You should change the root to your domain, and then change the author page. You can remove my specific post redirections and add in your own.

To pull the code to your machine, in the terminal or commad prompt type:
git clone https://github.com/crslade/wp2blogger-redirector.git


 Then you should have a wp2blogger-redirector directory.  Change into the directory and edit the config.ru file.  You can test it by typing in the terminal or command prompt: rackup.  This will start the server on your local machine.  Then in a browser goto http://localhost:9292. You should see "Nothing Here". You can then type the Wordpress URL after the :9292 (like http://localhost:9292/2011/09/blog-post/) and you should be redirected to the right URL (http://www.youdomain.com/2011/09/blog-post.html).  As you fix things, remember that your browser will cache the redirections, so you need to clear the browser cache if something goes wrong and you want to retest it.  Also, every time you make a change to config.ru, you will have to kill the server and start it up again (Control-C, and then type rackup again), or install the shotgun gem ("gem install shotgun").  Then you can launch the server by typing: shotgun.  It will start the server on 9393 instead of 9292, and you shouldn't need to restart it after changing the config.ru file (but if things aren't working you might want to try restarting it before you pull all your hair out).


You can also add in your old pictures by copying the /wp-content/uploads directory into the public directory. You will also need to add in the /wp-content to be served statically. To do this change line 3 in the config.ru file to be:
use Rack::Static, :urls => ['/index.html', '/favicon.ico', '/wp-content'], :root => 'public'


You can also test to make sure the server serves your old pictures. Heroku will only let you use 100MB in disk space.  So you might need to delete some pictures. Wordpress does store several sizes of pictures, and you can remove all of the sizes you don't use on your blog.


After you get the redirection server working locally, you can push it to Heroku.


To commit the changes locally, type:
git add -A
git commit -m "some message"


Then create an account on Heroku. You can follow these instructions to launch your server on Heroku.  Don't worry about bootstraping your database, you don't have one. In summary type the following:
heroku create
git push heroku master


If everything worked your redirection server should be live. Test it by typing heroku open, and you should see it in your browser. Remember the URL, because you will need it when changing your DNS.


Migrate your domain to blogger
Now you are ready to migrate your domain.  First create a CNAME record in your DNS to point old.domainname.com to Heroku, and follow these instructions for adding the subdomain to your Heroku app (you are probably not on the Cedar stack).  You only need to add one subdomain, not your full site.    While you are at it, you can now point your main domain (or a subdomain) at your new blog and edit your new blog's settings to add your domain.  It's under Settings -> Basic. You will also want to add a missing files host, and put in "old" (if you used old.domainname.com) for the name of the host.  Blogger has a link with instructions on how to set your domain name to point to Blogger that should appear as your change your settings.


If you are using OpenDNS, you can go here, and check their cache. After you check, you can request a cache refresh and it will update the DNS settings from your registrar. (It might take a couple of hours for your registrar to update it's DNS servers.) You will also have to refresh your local DNS cache by rebooting your machine.  Your domain should be pointing to your new blog. Remember, it takes a couple of days for the DNS changes to propagate to everyone, but with OpenDNS you can try it a lot faster.  You should leave your old blog up for at least a few days, so if your readers haven't gotten the DNS update, they will still see your old blog.  You might want to disable comments, because they will not migrate over to your new blog.


Migrate Disqus comments
You can now install Disqus on your Blogger blog, by going to disqus.com, logging in, clicking on your blog, and then clicking the install tab.  Click on blogger, and it will install it to your blog. You will then need to edit your layout in Blogger, to add the widget (preferably on the bottom). You should see Disqus take over for your comments, but all of your old comments will be gone.  Now, back in Disqus, go to tools, migrate threads.  You can try the redirect crawler, if your redirects are all set up. You can also upload a URL map.  Once done, your comments should re-appear.


Unfortunately, your likes and +1's will be reset. You should now be up and ready to to go. Hopefully, Google+ integration, and a free hosting solution will make all the work worth it. If you have any questions, just leave them in the comments.

Sunday, October 23, 2011

I Moved to Blogger

Since it has been another year, it was time for me to reanalyze how I am hosting this blog. A little while ago, I read a blog post by Jesse Stay about switching to Blogger. What I remembered the most was that he has able to host his blog on Blogger for free. Since I don't use this blog much, I don't feel like it is worth it to pay too much for hosting.  I have been hosting it with a self-hosted Wordpress install, and as Jesse points out, it was too time consuming keeping it up to date and running smoothly.  Without the super cache plugin set up and configured correctly, I was getting up to 3 seconds per page load which would never survive a high traffic day. Even with the cache setup correctly, I still even doubted that my blog would withstand a heavy traffic day.

I could have moved my blog to Wordpress.com, but that comes with ads and additional fees that Jesse details in his blog post. Some other options I looked at were using Github pages and using toto hosted by Heroku. I really like the idea of having a git repository being the source for my blog, but I couldn't format my old posts in the markdown, and none of the scripts translated things over correctly. It ended up being too much effort to transfer my old blog posts over so decided to try blogger. (I was really hoping to edit my posts with a plan old text editor too.)

In less than 5 minutes, I had all of my posts and comments transfered over to Blogger, so I was sold. It did take a little while to configure things the way I wanted, and then I had to redirect my old Wordpress style links to my new blogger links. I plan on doing a post in the near future describing the process of transferring my Wordpress blog to Blogger (including how to redirect your links), so stay turned.

I do want to explain how I set up some features (usually supplied by a plugin) that I had on Wordpress on Blogger.  First of all, I have been using Disqus as my comment engine on my Wordpress blog.  As you post comments on a lot of different sites, there isn't a way to aggregate all of your comments together.  Disqus lets your readers create a single profile they can use to comment on every site that uses Disqus, that way all of your users' comments can be aggregated together. It also allows users to use their Facebook or Twitter accounts to comment.  Lots of sites are using it, including newspapers and Foxnews.

Disqus installation is easy. Create an account on Disqus, then add a new site. Fill in your url and look through all the settings to make sure it's setup the way you want. Import your current comments by going to Tools -> Import/Export. Then click on the install tab and click on Blogger. It will ask you to select your blog. Once you finished with that, you will have a Disqus widget. Add it to your layout (preferably on the bottom part).  You should see Disqus take over for your comments.

The next feature is Feedburner, which will optimize your feed for each of your subscribers.  Your feed is what Google Reader and other news/feed readers use to know when your blog has been updated. Since FeedBurner is owned by Google, installation is easy. Go to Feedburner.com and create an account. Then burn your feed by putting in your URL (make sure you select your posts' feed and not your comments' feeds).  It will also let you assign your feed a url. Then, go to your Blogger settings, under other.  You will see a "Post Feed Redirect URL" setting. Paste in your Feedburner feed URL. Now your subscribers should be getting their feeds from Feedburner.  To test it go to http://yourblog.blogspot.com/feeds/posts/default (or  http://yourblogaddress.com/feeds/posts/default if you have your own domain).  You should be redirected to Feedburner and see your posts.

Finally, I wanted to add a Facebook like button to my posts. By default, I already had the +1 button (Google+) and a Twitter button but no Facebook button. To get a Facebook like button follow these instructions. There are some old instructions that don't work for the current layouts so be careful.  If your like count is strangely high (around 16,000 likes), than your readers will be liking some page called post.url instead of your post!

Sorry for the long post, but I hope the information is helpful.

Wednesday, September 21, 2011

Rails Editors and the Command Line

One of the first questions you will need to answer when starting with Ruby on Rails, is what editor should you use.  For the mac, it has been TextMate.  I haven't really liked the IDE's for Ruby on Rails (although I never really used them).  So, I needed to find a good text editor for Windows.  Turns out Sublime Text looks like the best one I was able to find.  It works in Windows, Mac, and in Linux.  It is a lot like TextMate, and might even surpass it in features.  I almost switched from TextMate to Sublime, but I didn't because the snippets and plugins aren't quite up to par with TextMate's.  However, if TextMate 2 doesn't come out soon, Sublime will probably surpass it soon.  Must have features are snippets, and a sidebar file browser.  Both TextMate and Sublime have them.  (Of  course if you're a vi or emacs pro, than stick with it.  None of my students are.)  My only question is, should my students pay for it?  I told them they should when they start making money using it.

I also notice that none of my students had any command line experience.  Thinking about it, I had to learn the command line (in Linux) in a Software Engineering class that made us learn Linux and C++ pretty much on our own and then write two substantial programs in C++.  Although frustrating at times, it turned out to be a very good class.  We never teach the command line in any of our classes.  I got a big blank stare when I mentioned make. The downside to only teaching with IDE's.  Luckily, I need to revamp my Operating Systems class.  Maybe I will make it a lot like my Software Engineering class.

Update:
See this post for more information about editors: http://www.christopherslade.com/2011/10/another-rails-editor-and-user.html