Fork me on GitHub

0.1.0: The Obligatory ‘Blog’ Demo

Ok, because every good framework should tell you how to create a blog, why should Mack be any different? Let’s start off with the basics. Is Mack installed? If not, here’s how:

$ sudo gem install mack

Great! Before we move on, make sure that the gem you installed is at LEAST version 0.1.0, otherwise, you’re not going to get very far in this tutorial. Now, let’s move on. Now let’s generate our kick ass new blog, and since we’re going to need some sort of database support for our blog, we’ll configure it to use DataMapper. If you don’t have DataMapper installed, please head over to http://datamapper.org to find out how to install it. Mack has support for ActiveRecord as well, but it’s just easier to get DataMapper going because you don’t have to deal with migrations.

$ mack my_kick_ass_blog -o data_mapper
$ cd my_kick_ass_blog

That should’ve created a whole bunch of files and folders for your blog. Now let’s generate some scaffold code for our blog:

$ rake generate:scaffold name=posts

That should’ve created even more files for you. One of those files is app/models/post.rb, let’s open that up, so we can edit it for DataMapper.

Edit the file so it looks something like this:

class Post < DataMapper::Base
  property :title, :string
  property :email, :string
  property :body, :text
  property :created_at, :datetime
  property :updated_at, :datetime

  validates_presence_of :title
  validates_presence_of :body
  validates_presence_of :email
end

Now, I’m not going to go into detail as to what that’s doing, that’s for the guys at DataMapper to explain. Before we move on to the next step, you’ll probably want to crack open config/database.yml and edit it so it the paths to your database are correct, you’ll probably also want to go to your database system and make sure that the database name you configured in your config/database.yml is created, otherwise this will be a very short trip. I’ll wait while you do that. Finished, great! Let’s move on.

We need to now open a Mack console so we can create the tables needed for our blog.

$ rake console
$ Post.table.create!
$ exit

Ok, we should now have a posts table in our new database. Isn’t life wonderful? We’re so close to showing the world how wonderful we are as developers.

Now let’s edit our views, so they look something like this:

app/views/posts/index.html.erb:

<h1>Listing posts</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Body</th>
    <th>Email</th>
  </tr>

<% for post in @posts %>
  <tr>
    <td><%=post.title %></td>
    <td><%=post.body %></td>
    <td><%=post.email %></td>
    <td><%= link_to("Show", posts_show_url(:id => post.id)) %></td>
    <td><%= link_to("Edit", posts_edit_url(:id => post.id)) %></td>
    <td><%= link_to("Delete", posts_delete_url(:id => post.id), :method => :delete, :confirm => "Are you sure?") %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to("New Post", posts_new_url) %>

app/views/posts/edit.html.erb:

<h1>Edit post</h1>

<%= error_messages_for :post %>

<form action="<%= posts_update_url(:id => @post.id) %>" class="edit_post" id="edit_post" method="post">
  <input type="hidden" name="_method" value="put">
  <p>
    <b>Title</b><br />
    <input id="post_title" name="post[title]" size="30" type="text" value="<%= @post.title %>" />
  </p>

  <p>
    <b>Body</b><br />
    <textarea id="post_body" name="post[body]"><%= @post.body %></textarea>
  </p>

  <p>
    <b>Email</b><br />
    <input id="post_email" name="post[email]" size="30" type="text" value="<%= @post.email %>" />
  </p>

  <p>
    <input id="post_submit" name="commit" type="submit" value="Create" />
  </p>
</form>

<%= link_to("Back", posts_index_url) %>

app/views/posts/show.html.erb:

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Body:</b>
  <%= @post.body %>
</p>

<p>
  <b>Email:</b>
  <%= @post.email %>
</p>

<p>
  <b>Created at:</b>
  <%= @post.created_at %>
</p>

<p>
  <b>Updated at:</b>
  <%= @post.updated_at %>
</p>

<%= link_to("Edit", posts_edit_url(:id => @post.id)) %> |
<%= link_to("Back", posts_index_url) %>

app/views/posts/new.html.erb:

<h1>New post</h1>

<%= error_messages_for :post %>

<form action="<%= posts_create_url %>" class="new_post" id="new_post" method="post">
  <p>
    <b>Title</b><br />
    <input id="post_title" name="post[title]" size="30" type="text" value="<%= @post.title %>" />
  </p>

  <p>
    <b>Body</b><br />
    <textarea id="post_body" name="post[body]"><%= @post.body %></textarea>
  </p>

  <p>
    <b>Email</b><br />
    <input id="post_email" name="post[email]" size="30" type="text" value="<%= @post.email %>" />
  </p>

  <p>
    <input id="post_submit" name="commit" type="submit" value="Create" />
  </p>
</form>

<%= link_to("Back", posts_index_url) %>

Ok, so now we’ve created our forms, and setup our index page. Let’s actually go to the site and see it all works!

First we need to start the server:

$ rake server

Now let’s head on over to http://localhost:3000/posts and see what we’ve got. You should see a page that looks something like this:

Blog Demo 1

Now let’s click on that ‘New Post’ link and fill out the form:

Blog Demo 2

Now, let’s hit that wonderful ‘Create’ button and see what happens!

Blog Demo 3

Congrats! You just created your first blog post! Now let’s head back to http://localhost:3000/posts and see what we’ve got.

Blog Demo 4

Wonderful! Now all that’s left to do is to set our home page to our posts index page. Let’s open up our config/routes.rb and edit the following line:

r.home_page "/", :controller => :default, :action => :index

so that it’s now:

r.home_page "/", :controller => :posts, :action => :index

Now all you have to do is to restart your server and Bob’s your uncle when you hit http://localhost:3000 again you should your fantastic posts index page.

This concludes our brief introductory tutorial on getting going on Mack. Obviously Mack does a lot more, and I highly encourage you to read the RDoc to find out more about what it can do.

Enjoy.

Tags: , , , , , ,

3 Responses to “0.1.0: The Obligatory ‘Blog’ Demo”

  1. Dinooz Says:

    I wonder why the app/models/post.rb need to include the DB Definition.

    class Post This can create the properly migration file or update the app/models/post.rb file automatically.

    class Post < DataMapper::Base
    property :title, :string
    property :email, :string
    property :body, :text
    property :created_at, :datetime
    property :updated_at, :datetime

    validates_presence_of :title
    validates_presence_of :body
    validates_presence_of :email
    end

    Also create the files with the properly content !!!.

    app/views/posts/index.html.erb
    app/views/posts/edit.html.erb
    app/views/posts/show.html.erb
    app/views/posts/new.html.erb

    So what is left for you to do just update your routes file and start your web server. =)

    Note
    when you mention posts_index_url, does exist the post_index_path also ???
    What about the comments, can you elaborate on how to add a comments relation in this framework ???
    How can enable the usabe of nested resources ???
    Any possible ideas on implement basic themes/skins ???

    So far looks good , I think will give a try keep doing the good job.

  2. Mark Bates Says:

    Hi Dinooz, thanks for the feedback. The reason the schema information is in the model, is because that’s the preferred way of using DataMapper. If you’re using ActiveRecord, then you can definitely put it in a migration.

    I much prefer the use of migrations, then the DataMapper approach, but using DataMapper made for an easier demo. :)

    “Also create the files with the properly content !!!.

    app/views/posts/index.html.erb
    app/views/posts/edit.html.erb
    app/views/posts/show.html.erb
    app/views/posts/new.html.erb”

    I think what you mean by that is why doesn’t Mack create all the form elements when you run scaffold like Rails does. Well, there’s two reasons for that.

    One, is that Mack is an ORM agnostic framework. That means you should be able to use any ORM you wish with it. There are basic hooks for ActiveRecord and DataMapper, however.

    Two, is that I’m still very much so working on the core components of the framework. The scaffold generator is really there to just ‘lay things out’ a bit for you. Down the line when I have more time, and a more of the core feature set implemented I plan on returning to this, and trying to improve upon the scaffold generator for both DataMapper and ActiveRecord. Of course, with that said, I’m looking for other contributors, so if you’re interested and want to write that part of the code, I’m happy to chat with you. :)

    ‘posts_index_url’ gives you ‘/posts’, so it effectively does with ‘posts_index_path’ does in Rails. It’s absolute, but does not give you the full domain path. This was done for a couple of reasons, the big one being that by not giving you the full domain path, you’re able to use the routing methods without a request. This makes it easy to use them in Rake tasks, emails, etc… something that Rails has a big problem with. If you really need the domain on there, then write a helper method to wrap the *_url methods, or just override the ‘link_to’ method.

    Currently there is no support for nested resources. That doesn’t mean there won’t be support for them down the line, it’s just right now, I’m one guy, and I’m trying to write the core features, and the features that are different from Rails, before I write things like nested resources.

    As far as skins/themes are concerned, that’s actually on my list of things to tackle. It’s a big need the company I am working for needs, and something that will be at the heart of Mack in a future release. Right now there is the standard layout support, similar to Rails.

    I hope that answers your questions. Please feel free and hit me up with more. I’m always welcome to feedback, I think it makes for a better code base in the end.

    Thanks Dinooz.

  3. Adding RSS/xml feeds to our Blog demo — Mack Framework Says:

    [...] as you remember a while back we created a simple blog using mack, http://www.mackframework.com/2008/03/04/the-obligatory-blog-demo/. Well now it’s time to add the all important RSS/xml feed to [...]