soledad penadés
repeat 4[fd 100 rt 90]

My first application in Ruby On Rails

It's time to get serious with RoR! I finally decided that I would do some useful application to really learn how it works, and as I'm quite bored of doing applications for managing text (whether they are intranets, cms's, etc), I opted for recreating my old gallery script (which I did in php) in RoR. So I'm playing with images instead of just text.

This will give me the opportunity to test lots of extra functionalities of the framework, such as the famous integrated javascript effects, ajax, etc, as I want to improve my old gallery script, not simply redo it as it was in php.
So first thing I did was to make sure that I had a working updated version of ruby and rails. The best tutorial I have found which worked perfectly for me is this one: Building Ruby, Rails, LightTPD, and MySQL on Tiger, by Hivelogic. (Although I simply used it to update Ruby and Rails, but didn't build Lighttpd or Mysql).

With that I could be able to follow the recent tutorials and techniques, specially because I wanted to learn how to use a Login Generator, and there was something in the version of Ruby that I had which prevented me to install it (some lib error was thrown each time).

So my app has two parts, a public and an admin one (which was quite easy to anticipate). Once I managed to create the basic structure of controllers and models for the admin, I added the login functionality, as I don't want everybody to get into my app and modify pictures in there.

I used the Acts As Authenticated plugin, although I couldn't manage to get all the functionality that I expected so I took a look at the code of the Sudoku On Rails v5 tutorial at SobreRailes (sorry, spanish only!) and completed it with the missing functionality (basically, it was the activation stuff which was not generated, don't know why). It was sweet and smooth. If I had had to write it manually I would have spent at least three days.

Then I wanted to have support for uploading images. At all, this is about pictures! So after some researching I found a little plugin called FileColumn which allows to get picture uploads working quite quickly, including resizing and automatic deletion if the associated record gets deleted (no more orphan files in the filesystem because of a poor integration between the models and their associated data! hooray!). But for the resizing stuff and thumbnails generation, I needed to have RMagick installed in my system, which I didn't have. So I found a nice tutorial in the RMagick site, which explained how to compile and install RMagick and GraphicsMagick on mac os x, as well as all the libraries on which they depend, as RMagick is like a bridge between ruby and GraphicsMagick. This step took me a bit of time, specially the step of compiling ghostscript, but as I followed the instructions exactly as specified, I had everything working at the end and I could generate thumbnails and a resized version when uploading images. (Also I was also reading one of the supercool books about the tube that mr.doob gave me for my birthday so time flew fastly! :D )
If I had had to do this in php even using the best helpers that I could, I would have spent at least one week.

Then I prepared a draft version of the public interface. Now I can navigate between pictures and albums, using thumbnails and the big images as well, it shows the descriptions for the album or picture, where available. The code is so simple and beatiful that I could get it tattooed. Or maybe do a t-shirt with it, see an example for getting the data of one album:

def album
@album = Album.find(params[:id])
unless @album.nil?
@pictures = @album.pictures
end
end

No more embedded SQL in the middle of the code, no need for manually loading classes which abstract the DB information for the rest of the php code and try to provide a way of artificial modelization of the data, no need to spend time reinventing the wheel!! :D

As I specified in the models that one album has pictures (has_many : pictures), and that each picture belongs to one album (belongs_to :album), RoR is intelligent enough to deduct things from the database and save me that precious time (as it sees an album_id column in a picture row… it obviously is pointing to an album row!).

Unfortunately by the moment the front end is extremely basic, it's pure html with little css applied to it. In the next iteration I will do tasks such as find out how to redefine part of the behaviour of the FileColumn to make it update the associated images too if I upload another image when editing a picture, beautify the front end, and maybe start with those nice ajax capabilities which are supposedly quite easy to use with RoR. It will also be nice to learn how to do the fantastic unit tests with the support that this framework provides, so all in all this promises to be very exciting.

Conclussion: I absolutely love Ruby on Rails.

Stay tuned for a next release - as I already got a port for installing it on my host, it can be whenever I get a decent working version!.

UPDATE: As some people want to take a look (miguev ;) ) I have uploaded the relevant code of the current version. It just has the important things i.e. the models definition, etc. If you want to have a working application you will need to create it first with the usual rails name_of_your_app, configure the database, etc, and then use this code where appropiate. It won't work by itself :P This is just for showing how things are linked between them: cl1ck version 0.1

// 13 responses to My first application in Ruby On Rails

sergeeo
sergeeo
20060501

Mmm al fin algo que te gusta… ¡¡Siempre te estás quejando del mundo!! :)

sole
sole
20060501

Pedorro :P
Si has estado haciendo basura con php durante anyos, es imposible que no te guste Ruby On Rails. Es delicioso.

miguev
miguev
20060501

Me gustaría ver ese código, ahora que todavía es pequeño y sencillo. Estoy empezando con Ruby con intención de pasar el PHP a Rails,
me ayudaría mucho ver un código funcional sencillo.
Porfa, porfa, déjame probarlo :-)

HeV
HeV
20060501

Muy bien Sole, siempre probando cosas nuevas (o no tan nuevas) a ver si ahora cuando pasen los exámenes me pongo yo también.

sole
sole
20060501

No tan nuevas :D
Lo vas a disfrutar, una vez empiezas a situarte es genial.

miguev
miguev
20060501

Thanks a lot! :-)

Epaminondas Pantulis
Epaminondas Pantulis
20060504

¡Vaya! ¡Me alegra que te haya servido el código del Sudoku on Rails! (la verdad es no de ja de darme un poco de verguenza, no es tampoco como para estar orgulloso de él).

sole
sole
20060504

Lo importante es el concepto, Epaminondas! Tu pagina esta muy bien, me ha animado mucho para meterme en RoR. Gracias por ella!

miguev
miguev
20060512

Perdona que sea un poco pesando pero hay algo que no me cuadra, ¿cómo se supone que voy a crear las tablas? No encuentro los esquemas *.sql ni en tu cl1ck ni en el plugin de autenticación recién bajado del SVN. ¿Acaso las tablas se crean automáticamente a partir del modelo con algún comando que no encuentro? ¿Me estoy perdiendo algo? Gracias por todo :-)

sole
sole
20060512

No, si las tablas no estan ahi XD
Pensaba que solo querias ver el codigo…

miguev
miguev
20060512

Wow, que rapidez. Además de verlo me gustaría probarlo, para lo cual necesito las tablas. Pero más me preocupa que los plugins como Acts as Authenticated no incluyan los esquemas de sus tablas, ¿cómo te las apañas entonces para crear las tablas?

sole
sole
20060512

Bueno, los plugins no generaban bien el codigo asi que segui el ejemplo de sor para completar con el codigo que me hacia falta (viendo su codigo fuente), especialmente el de los modelos.
Luego creo que algunos campos de la tabla tambien los cree a mano. Pero vamos, si que consegui llegar a generar modelos y tablas (creo que con las migraciones). El unico detalle es que no tenian lo de activar las cuentas con un e-mail, etc…
A ver si puedo pasarte un volcado de mi estructura, asi lo miras. Es que desde la ultima version he cambiado cosas y como lo he hecho un poco al tuntun no he guardado copia (que verguenza eh!).
Lo que pasa es que aqui no lo tengo, lo hare cuando llegue a mi mac :D

miguev
miguev
20060512

Entendido, voy a seguir jugando un rato con el plugin a ver si con las migraciones voy tirando mientras espero ver tu código ;-)

You came late to the party

Comments are closed, but if you want to comment anything about this entry please let me know using the info in this page.