3. Howto make a (small) messaging app
3.1 Foreword
Ajax is a really fun thing, it allows you to do some kind of magic in your web apps and with Rails 0.11 it’s much more easier than what you think … when you understand how to do.
This small howto will show you how to build a Ajax ready app by showing you : rails ajax helpers, javascript basics, and html things you need to do.
The app is a small message posting thing, people can post messages and delete them. Really really simple.
All the code is under the MIT licence. This howto is under the CC by SA licence.
3.2 11.0 bugs
Fixes
There is some small bugs in rails 0.11.0, most of them have been fixed in the repos but as I’m not using non stable rails releases I’ve bumped into these bugs. Here they are :
- Toggle.display doesn’t display or undisplay an html node : http://weblog.rubyonrails.com/archives/2005/03/22/rails-0110-ajax-pagination-non-vhost-incoming-mail/#comment-1613 fix : In prototype.js lines 61 & 62 need to be changed from:
for (var i = 0; i < elements.length; i++) {
var element = $(elements[i]);
to:
for (var i = 0; i < arguments.length; i++) {
var element = $(arguments[i]);
Note : it seems that you are then obliged to use : <%= javascript_include_tag "prototype" %> in you head and not <%= define_javascript_functions %>.
- Dispatch.fcgi : copy it from an older app or get one from the svn repos
If you are using Rails 11.1 and later, you shouldn’t have any problem.
3.3 Database structure
Mysql
Create a database called ajax_test (for example) and use the following sql to create the messages table :
CREATE TABLE `messages` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) default NULL,
`note` text,
`user` varchar(255) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
Others DBs
Well if anyone could add the schemas …
3.4 Creating the app
%: rails your_app
%: cd your_app
Edit the config/database.yml file to make it fit with your config.