Welcome to the first part of the tutorial. We will start a new hobo project, set up a model to hold blog entries, and add markdown formatting.
To create our new hobo project, we use this command:
$ hobo blog
You will see a load of output. This is the scripts of hobo and rails setting up everything we need to get started. We will now create a model to hold our posts with this command:
$ cd blog
$ script/generate hobo_model_resource entry name:string body_markdown:text body_html:html
You’ll be presented with more output. Hobo’s scripts have created several things for us. The most important at the moment is a new model, entry, which has three fields: A string field called name, a text field called body_markdown, and a html field called body_html. What’s the difference between text and html fields? Text fields are escaped when viewed. They are the same otherwise. Let’s make it so our posts are can be written in markdown. This is our first bit of coding! Open a new file in the project directory called app/models/markdown_wrapper.rb and insert the following ruby:
require 'rubygems'
require 'maruku'
class MarkdownWrapper
def before_save(record)
record.body_html = Maruku.new(record.body_markdown).to_html
end
end
What this little piece of code does is every time our model is about to be saved, the text in body_text will be compiled into HTML by maruku and placed in body_html. There is one more thing to do to start to use it. Open app/models/entry.rb and add a line of before_save MarkdownWrapper.new after the line that reads hobo_model. Your code should look something like this:
class Entry < ActiveRecord::Base
hobo_model # Don't put anything above this
before_save MarkdownWrapper.new
fields do
name :string
body_markdown :text
body_html :html
timestamps
end
# --- Permissions --- #
def create_permitted?
acting_user.administrator?
end
def update_permitted?
acting_user.administrator?
end
def destroy_permitted?
acting_user.administrator?
end
def view_permitted?(field)
true
end
end
See our fields in the fields do block? The timestamps are created_at and updated_at datetime fields, which are added by hobo. Now all we have to do is apply the changes to the database. We do that with this command:
$ script/generate hobo_migration
Hobo will generate the database migration for us. You will see that it is creating a users table in addition to our entries table. Enter m to save the migration and apply it to our database. It will ask you to name the migration. This can be whatever you want. I like to use initial on the first one. Let’s fire up the development server and take a look!
$ script/server
Navigate to http://localhost:3000/ and you’ll see the new hobo app! It will have you create a new administrator user. Then, you can create your blog posts! A lot of functionality has been accomplished with a few lines, but the interface is awkward. We will fix this in the next portion of the tutorial.
Comments
-
On February 24, 2010 05:28, Banjoey said:OK, so I just tried renaming the class from Entry to BlogEntry, and that seemed to work. Is Entry a class name in Hobo or Rails or something?
Sorry, Rails Newb error here I'm sure. -
On February 25, 2010 15:59, Bret said:Indeed you probably have some gem loaded that already defines the 'Entry' class:
http://jackndempsey.blogspot.com/2008/06/superclass-mismatch.html
Naming things in Ruby .... fraught w/ peril ;)
Followed the directions exactly, and when I tried to do the migration, I keep getting this error:
rake aborted!
An error has occurred, this and all later migrations canceled:
superclass mismatch for class Entry
./db/migrate//20100224043642_entry.rb:1
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load_without_new_constant_marking'
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load'
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load'
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-2.3.5/lib/active_record/migration.rb:373:in `load_migration'
/Users/jbarkley/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-2.3.5/lib/active_record/migration.rb:369:in `migration'
(__DELEGATION__):2:in `migrate'
There's more to it, but figured I'd save your blog from the nastiness. Any ideas what the problem might be? I had this problem before with another sample project, but changing one of my field names seemed to correct the issue. I have tried changing all 3 field names and that does not help. I just upgraded to Hobo 1.0. I have started over at least a couple of times and nothing works, and I even resorted to copy/pasting the Entry class above into my own. Advice?