At Rails Girls Next we actually didn’t do any Rails at all. There were three components which took up the entire day and they were 1) Coding in pure ruby 2) Testing with minispec and 3) Introduction to Sinatra.
I hadn’t heard about Sinatra until the day before, when my Coder Friend suggested I use it for my new flight app idea. Apparently Sinatra is about 1% the size of rails – so it’s lightweight. Having said that, it’s just as easy, if not easier, to set something up in it.
To learn more, I did these tutorials about Sinatra, on Net Tuts, and I’m linking to them here, because actually navigating between them isn’t so easy.
Unlike Rails, when you start a Sinatra app, you don’t have all files and everything set up. You just install your Sinatra gem, open a new file and type in require ‘Sinatra’. This is the start of your Sinatra app.
So in that base file that you just opened, you can write all your ruby. Which in this case included;
- Requiring gems
- Helpers – e.g. the thing for XSS protection below
- Linking to a database
You can also make a public folder to put in your css files, images, etc.
The other folder you can create is a ‘views’ folder, where you put all your html pages (saved as erb files).
EDIT: I have just discovered that if you want to add external css and js files, like in bootstrap, you need to put them in another folder in your root directory, and call it ‘public’.
I have now done the tute, and I am going to tweak it to make it my own, BUT, I still have a few questions to ask someone at the next opportunity:
Questions About Sinatra / Things I need to investigate more
You may be wondering “what about SQL injections?” Well, DataMapper handles that for us just as long as we use DataMapper’s methods for getting data from the database (ie. not executing raw SQL).
- Must look up what they mean by “Using DataMapper’s Methods”
- How do I organise my base Sinatra file? Should I have several of them when they get very big? Should the database connection go in a separate file?
- How do I know what order to put my routes in? E.g. the tutorial said I couldn’t put my RSS one above my get :id one – can I not put anything above that get :id one?
Just remember to escape all user-submitted data when creating other web apps in the future!
And finally, this quote, makes me think I need to make a big list of all the “And just remember…”. Maybe a checklist!