A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Thursday, November 14, 2013

Web Apprentice - Tutorials on Web Technologies

The range of web technologies that are available to web designers and developers is just incredible right now - from maps and graphs to translation and speech recognition.

Many of these are easy to incorporate into your sites but getting started can be confusing. There are examples and tutorials that are obsolete or needlessly complex - I've written some of them myself !

I want to help people get up and running with simple examples that they can use in their work today.

So I have created Web Apprentice (http://apprentice.craic.com) - a site that contains clear, straightforward tutorials on web technologies across the spectrum.

Tutorials range from simple embedding of Twitter feeds or Maps through to Typography, Translation and Geolocation.

Each tutorial has a self-contained live demo and all the code is available and free to use. Tutorials walk you through all the steps and explains what is going on in the code.

There are three levels of tutorial:

Basic - embedding widgets into your pages - no need to know JavaScript
Intermediate - involve some JavaScript
Advanced - complex JavaScript and Server side programming - may involve new technologies that are not in all browsers.

The goal is to add new tutorials every couple of weeks at least and over time build the site into a destination for learning and applying web technologies.

Please check it out at http://apprentice.craic.com and let me know what you think.

Monday, November 11, 2013

Setting up a new Application on Heroku

You can set up a new web application on Heroku in several ways. Here is my preferred way:

1: Build and test your application locally

2: Set it up as a git repository and commit all changes

3: Go to your account on the Heroku web site and cerate your new app there with your preferred name.

4: In your local git repo run:

$ heroku git:remote -a <your-app-name>

The response should be 'Git remote heroku added'

5: Upload your code with:

$ git push heroku master

6: Finish up your Heroku configuration on the we b site

7: Import any existing database as directed by this article.

Tuesday, November 5, 2013

HABTM relationships in Rails 4

Rails 4 has changed the way has_and_belongs_to_many (HABTM) relationships are handled as well as introducing strong parameters in the controller as a replacement to having :attr_accessible in your model.

Here is how you set up HABTM with a joining table.

My models are Tutorial and Category - a tutorial can have more than one category and vice versa.

Both models already existed and I wanted to create the relationship between them.

1: Create a migration for the joining table

The default table name should be the plural of the two models, in alphabetical order, separated by an underscore. (You can give it any name you want - you just need to include that later in the model)

class CreateCategoriesTutorials < ActiveRecord::Migration
  def change
    create_table :categories_tutorials do |t|
      t.belongs_to :tutorial
      t.belongs_to :category

2: Add the relationships to the models

Previously you would have done this (in the Tutorial model):

has_many :categories_tutorials, :dependent => :destroy
has_many :categories, :through => :categories_tutorials, :uniq => true

That is now simplified to this:

has_and_belongs_to_many :categories

If you have a custom table name then specifiy it like this:

has_and_belongs_to_many :categories, join_table: :my_category_tutorial_table

The Category model has:

has_and_belongs_to_many :tutorials

The CategoryTutorial model has:

belongs_to :category
belongs_to :tutorial

In my example I want to specify Categories when I create a Tutorial. 

In order for this to work I need to allow the tutorials_controller to accept the category_ids parameter which is passed from the tutorial form.

At the bottom of the controller Rails has created this:

def tutorial_params
    params.require(:tutorial).permit(:title, :description, :status)

I need to add category_ids and have it set up as an array 

def tutorial_params
    params.require(:tutorial).permit(:title, :description, :status, :category_ids => [])

That last step is critical - if you miss this then the associated rows are not created in the database.

For more information on HABTM take a look at:


Archive of Tips