Category: Uncategorized

Rails for Designers

This is a short post to help designers who aren’t familiar with Rails understand their way around it enough to change things and work with developers. It assumes that you know CSS and HTML and aren’t scared of HTML that has embedded code in it.

The structure of a Rails app

Rails is a Model/View/Controller system. 

  • Models are business logic, and storing things in databases
  • Views are displaying the contents of the models
  • Controllers co-ordinate the Model and the View

Here is a picture of the directory structure of a Rails app:

The files you care about live under app and public:

And views also breaks down like this:

This application has an extra top-level directory, artwork, that isn’t standard. I used it to keep the artwork and design files under version control.

What the directories do:

  • Public. This is where the root of the application is for static content like CSS and JavaScript files.
  • App/Views. This is where the files are stored for rendering dynamic content from the application.
  • App/Views/Layouts. This is where the templates that surround the dynamic content are stored.

The example we show here is from before Rails introduced the performance enhancement of the asset pipeline, it complicates things a little and I will discuss it later. What I want do do first is explain the principles.

What things do

A layout file looks something like this:

<!DOCTYPE html>

<html>

<head>

  <title>Favorbite</title>

  <%= stylesheet_link_tag :all %>

  <%= javascript_include_tag :defaults %>

  <%= csrf_meta_tag %>

</head>

<body>

  <div id=”header”>

    <div class=”span8 offset8”>

      <h1>

        <a href=”/”><img src=”/images/favorbite-logo.png” alt=”Favorbite logo”></a>

      </h1>

   <%= yield :header %>

    </div>

  </div>

    <%= yield %>

</body>

</html>

This is a very simple wrapper. It’s HTML 5, of course. The <%= %> tags are used to embed code. The :all in stylesheet link tag lets us include all of the files in the public/stylesheets directory without having to specify them individually, or you can include them one by one if you need to. This changes with the asset pipleline (but let’s not worry about that now).

You can also just put in plain old HTML code to include whatever CSS you like, the helpers are a convenience and also help with cacheing and other wondrous magic web things. 

You can see two lines that I have made bold. Each contains the word yield. This is a Ruby construct that you can find out more about if you’re interested. The one that doesn’t have any arguments will render the appropriate dynamic content file. The one with :header will render any content that is in a block that has that name. For example

Hello, this is ordinary content

<% content_for :header do %>
   This will appear where the yield :header tag tag is, or not appear at all if there isn’t one.
This is how you define a block, using ‘do’
<% end %>
If we carry on here it will just add into the main text.

<%= “the string here will show in the text, without the = sign nothing will happen, the results of any Ruby code need %= to show” %>

Working out what gets displayed

Rails apps have routes go to controllers, and from there to the view depending on what the controller decides to do to render the request. The surrounding layout is set by the controller. For example http://myapp.com/chickens/1/edit is asking the router to find a controller called chickens, and call the method edit in it. This will then find the chicken with ID 1 and render it to be edited if all goes well.

The dynamic code for the edit will be in app/views/chickens/edit.html.erb (ERB being a way of embedding Ruby code), inside that file you may find code that looks like this:

<%= render :partial => “form” %>

Partials a little chunks of reusable code, because no directory was given it will be assumed to be in the same directory as the other chickens code, here app/views/chickens/_form.html.erb

The convention for a partial is it has a leading underscore in its name. If the controller doesn’t explicitly say what dynamic content to render the file that matches the route will be used:

  • …/chickens – This will go to index method and will look for a file called index
  • …/chickens/1 – This will try and show the chicken with ID 1 – method/view called show
  • …/chickens/new This will render a form where you can create a new chicken
  • …/chickens/1/edit Guess!

When you save a new chicken you land in the method create, when you update an existing chicken you land in the method update. If these methods succeed the convention is that it puts a flash message saying it succeeded and takes you to the show method. Sometimes you will need to look in the controller code to unpick this.

For example if you see:

render :action => “edit”

It will use the edit file to render, rather than whatever the default one would be for the method you are in.

Picking the layout

If the controller doesn’t explicitly set a layout it will use the one defined in app/views/layouts/application.html.erb.

Configuration by convention

As you can see Rails uses convention to say where things are and you don’t have trawl through configuration files (Java) or wade through heaps of confusing code where it’s hard to work out where the application ends and the data begins (PHP guys, looking at you here). 

The asset pipleline

This was introduced in Rails 3.2. There is an extra directory under app called assets. Rather than repeat the documentation I suggest you go here to find out how it works.

I hope this helps. As always if I have erred or not explained things to your satisfaction, please contact me and I will do my best to explain it better or correct any mistakes.

Thanks for reading.

Dead Time at East Didsbury

Sometimes time is dead time; you have to wait and hope that it will pass

Dangerous thought – how good it would be to edit your life away, but how would that be good?

Once lost it doesn’t come back, whatever it might be

So this moment is of waiting and not exactly enough, or not moving forward to where you would like to be:

So what?

Is it really that different from all the other moments?

This body waits for the next thing – driven by this mind

This mind is what cares about forward and back

This mind judges and measures

This mind does not see its own nature

So what is waiting?

What is impatient?

What is hurting?

What is sad?

Does it have a colour, shape or presence anywhere?

Can you touch it? Except maybe indirectly?

Why does it want so much?

Why does it need anything?

The thing it needs is not there.

It is closed and also open

The warm train arrives – the moment dies

False Analogy – meh

Just had a stupid Twitter spat with someone about the analogy that asking someone to predict the number of bugs is like asking a chef to predict the number of people who will fall ill.

It’s hard to discuss things in 140 characters, particularly if the individual is some kind of pedant who just can’t be wrong. I used the term false analogy, which was originally coined by the long-dead English philosopher John Stuart Mill. Mill meant that the analogy was being misapplied and did not fit where it was being used. However I was dealing with an über geek, who took the literal meaning of the term and started off a load of crap about how analogies are by definition false. Meh, I really must not assume that people know the same things I do. I’ve been using this term (without any problems) for years, but I also did study philosophy in my youth (30 odd years ago now).

My take was that we have more than 100,000 years of cooking expertise and 40 or so of software engineering. We’ve already been through the how do I cook that tuber full of starch in such a way it won’t poison me stage of our culinary development longer ago than most of us can conceive of. In software terms we’re still finding out that starch is good for us. Also most chefs, in my experience, don’t measure their success or failure by how ill their customers are. They measure by whether their customers come back to the restaurant, which is a result of the 100,000 years.

So, tried to make this point in 140 characters – mistake!

I got some stuff back about how our knowledge of germs and software are about the same. Still not true, actually, because there are a hell of a lot less people creating software than there are people who cook food and follow basic hygiene. But I can’t be bothered with this argument, I really can’t.

Some people just have to win, and aren’t short on off the cuff insults. So I unfollowed and blocked. Got better things to do, seriously!

The other thing I’ve noticed is that some people seem to have a supply of URL’s they need to make a point – and just throw them at you at any opportunity without even thinking. I’ve never used tumblr, but suspect that this is a symptom of using it. For example I mentioned there is a model for bugs. I really meant for how long things take, as in using kanban you can say things to the business like I can promise that a ticket takes between 10 and 15 days once it hits the started state. I got pointed at this(PDF) – I don’t care, all I’m interested in is shipping stuff that works. I leave the pedantic stuff full of meaningless definitions that don’t fit what I do to the people who care about this stuff.

Statistically, if you keep metrics about your kanban board, you can make predictions like this. You can employ different confidence limits depending on how important the predictability is. David Anderson talks about this in his Kanban Book at some length. But it’s not the kind of ISO crap, it’s predictably shipping useful stuff. I believe the book he’s writing at the moment covers it in more detail, because I heard this at his keynote at Agile Cambridge last month.

Statistics are interesting. You can say how long any particular job will take, just look at the distribution of how long it might take. That’s the way it rolls. But then I also studied statistics in my youth, and even have a B. Sc. in it.

Tweeting things like I’m surprised an agile coach would say this and lecturing a practising Buddhist about koans as analogies (although we don’t have them in the Tibetan tradition – and they aren’t analogies, but I don’t have the energy to bother explaining why) is more than a little insulting. But the interwebs are full of such comments.

In any event – that’s my take and I don’t care to discuss it with people who are going to make ill considered, insulting comments in 140 characters. Or I’ll have to go up to dickfinity on ya.

Plus I also changed my Twitter profile to spell agile with a lower case A.

Oh, and it should probably have been stupid hyperbole, not false analogy. But I didn’t want to start a row. More fool me.