Menu
abriraqui
  • reflexiones
  • publicaciones
  • hackership
  • experiencias
abriraqui

Doing a pet project in Rails (I)

Posted on November 11, 2012December 29, 2013

Tweet

After being in a railsgirls Berlin event, we decided that we wanted to learn more, so, @killerkiaraa, @performentz and me decided to meet and think about possible pet projects, we have a couple in mind, but we have to begin with something so we decided to try to improve the access to information given by the stressfaktor.squat.net site by being able to locate in a map the events, have them in a calendar and doing a better display of categories of the events, lets see if we manage to do it 😀

The first thing we did, was actually that a bit of brainstorming of what could be done, what could be “easily” done in a first project and we wanted to be something that was not just a test but actually create a useful project 🙂

The next thing was to think about the data model, what did we need, what were the relations between the classes, first we had this diagram drawn by hand 🙂 ….

Handmade diagram of the stressFaktor pet project

and then we did it using a cacoo tool, so we could all have it somewhere near in case needs change, in case we forget how things are

Diagram of the project done with cacoo.com

So first we create the app

$rails new stressFaktor

Since I want to use MySQL so that my dev environment is going to be as similar as the production one, I prefer to start directly using MySQL instead of sqlite3, so I commented in the Gemfile the line that said

#gem 'sqlite3'

then I created my MySQL database

$mysql -uroot -p

mysql> create database stressFaktor;

then I gave the rails user the permissions

mysql> grant all on stressFaktor.* to 'railsapp'@'localhost' identified by 'railsapp';

After having done this @performentz told be that there is a better option to use mysql database from the beginning, that is directly write, when creating the app

rails new --database=mysql projectname

Now to get starting I created a scaffold of events

$rails generate scaffold Event name:string date:date time:time description:string location_id:integer event_type_id:integer organiser_id:integer link:string

BUT it gives the following error


sonduk@sonduk:~/reposrails/stressFaktor$ rails generate scaffold event name:string date:date time:time description:string location_id:integer event_type_id:integer organiser_id:integer link:string
/home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/execjs-1.4.0/lib/execjs.rb:5:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/execjs-1.4.0/lib/execjs.rb:4:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:68:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:66:in `each'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:66:in `block in require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:55:in `each'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:55:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler.rb:128:in `require'
from /home/sonduk/reposrails/stressFaktor/config/application.rb:7:in `'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:24:in `require'
from /home/sonduk/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:24:in `'
from script/rails:6:in `require'
from script/rails:6:in `'

That is because I don’t have the nodejs package install in my Ubuntu, so I proceed to do so

$sudo apt-get install nodejs

In my config/database.yml I had already specify that I was going to use mysql2

development:
adapter: mysql2
database: stressFaktor
username: railsapp
password: railsapp
socket: /var/run/mysqld/mysqld.sock
pool: 5
timeout: 5000

So I thought everything was all right and I typed again

$rails generate scaffold event name:string date:date time:time description:string location_id:integer event_type_id:integer organiser_id:integer link:string

BUT again I got an error

$rails generate scaffold event name:string date:date time:time description:string location_id:integer event_type_id:integer organiser_id:integer link:string
invoke active_record
/home/sonduk/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.2.1/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.) (LoadError)

 

So I did as the error said

$ gem install activerecord-mysql2-adapter
Fetching: activerecord-mysql2-adapter-0.0.3.gem (100%)
Successfully installed activerecord-mysql2-adapter-0.0.3
1 gem installed
Installing ri documentation for activerecord-mysql2-adapter-0.0.3...
Installing RDoc documentation for activerecord-mysql2-adapter-0.0.3...

Once the gem is installed you have to add it to the Gemfile, so edit it and add a line that says

gem 'mysql2'

Remember that you don’t need to do this if you create the app by saying

rails new --database=mysql projectname

Now, if everything has gone ok, you are ready to generate the scaffold

$rails generate scaffold Event name:string date:date time:time description:string location_id:integer event_type_id:integer organiser_id:integer link:string
invoke active_record
create db/migrate/20121109111504_create_events.rb
create app/models/event.rb
invoke test_unit
create test/unit/event_test.rb
create test/fixtures/events.yml
invoke resource_route
route resources :events
invoke scaffold_controller
create app/controllers/events_controller.rb
invoke erb
create app/views/events
create app/views/events/index.html.erb
create app/views/events/edit.html.erb
create app/views/events/show.html.erb
create app/views/events/new.html.erb
create app/views/events/_form.html.erb
invoke test_unit
create test/functional/events_controller_test.rb
invoke helper
create app/helpers/events_helper.rb
invoke test_unit
create test/unit/helpers/events_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/events.js.coffee
invoke scss
create app/assets/stylesheets/events.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss

 

and so we create the TypeEvent, Description, Location, Category, Organizer

for each we do the following, lets use Organizer as an example

sonduk@sonduk:~/reposrails/stressFaktor$ rails generate scaffold Organizer name:string

after generating each scaffold we proceed to execute the migration

$rake db:migrate

So that in each case the table and its fields are created in the database.

End of first day 🙂

©2023 abriraqui | Powered by SuperbThemes & WordPress