Ruby On Rails Starter Kit

33
Start Ruby On Rails Starter KIT score: 00000 0 XP

Transcript of Ruby On Rails Starter Kit

Page 1: Ruby On Rails Starter Kit

Start

Ruby On Rails Starter KIT

score: 000000 XP

Page 2: Ruby On Rails Starter Kit

Level #1

Introduction

Page 3: Ruby On Rails Starter Kit

score: 000000 XP

What is ruby?

Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.Matsumoto has said that Ruby is designed for programmer productivity and fun, following the principles of good user interface design

What is rails?

Ruby on Rails, or simply Rails, is a web application framework written in Ruby under MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages.

Page 4: Ruby On Rails Starter Kit

score: 000000 XP

Controller

ViewModel

Page 5: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 6: Ruby On Rails Starter Kit

Level #2

Installing Rails

Page 7: Ruby On Rails Starter Kit

score: 000001 XP

Windows:

@RailsInstaller provide package included all what you need to start your first project with Rails ( http://railsinstaller.org )

Linux&Mac:

You can install Rails with RVM (Ruby version manager) with a simple two commands:$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3$ \curl -sSL https://get.rvm.io | bash -s stable --rails

Page 8: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 9: Ruby On Rails Starter Kit

Level #2

Build your first app

Page 10: Ruby On Rails Starter Kit

score: 000002 XP

You can create a new app called (blog) by running the following CMD:

$ rails new blog

Now we need to change the directory to our Rails App

$ cd blog

start the rails server by running

$ rails s

You can find your app on the following link below:

$ http://localhost:3000

Page 11: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 12: Ruby On Rails Starter Kit

score: 000003 XP

You can create a new app called (blog) by running the following CMD:

$ rails new blog

Now we need to change the directory to our Rails App

$ cd blog

start the rails server by running

$ rails s

You can find your app on the following link below:

$ http://localhost:3000

Page 13: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 14: Ruby On Rails Starter Kit

Level #3

Start Scaffolding now

Page 15: Ruby On Rails Starter Kit

score: 000004 XP

So to make it easier creating CRUD system there is something called scaffolding, rails have the philosophy of DRY so this will save your time

$ rails generate scaffold post title:string content:text picture:string

Each time you create a model in rails you should migrate yourdatebase with:

$ rake db:migrate

Now check your posts page in your browesr in the followinglink:

$ http://localhost:3000/posts

Page 16: Ruby On Rails Starter Kit

+2 XPNew level has been unlocked

Page 17: Ruby On Rails Starter Kit

Level #4

Upload Pictures

Page 18: Ruby On Rails Starter Kit

score: 000006 XP

Firstable lets Open up the Gemfile wish is located in the root directory of your app and add this line:

gem 'carrierwave'

Then install the Carrierwave gem:

$ bundle install

After installing the Carrierwave gem now you should create your first Uploader, this will create picture_uploader.rb file in app/uploaders

$ rails generate uploader Picture

Page 19: Ruby On Rails Starter Kit

score: 000006 XP

Then lets mount our uploader to the Poste Modeleapp/models/poste.rb

mount_uploader :picture, PostUploader

Then lets mount our uploader to the Poste Modeleapp/models/poste.rb

mount_uploader :picture, PostUploader

Now we should update our poste form

<%= f.text_field :picture %><%= f.file_field :picture %>

And we should update the show page also

<%= image_tag(@post.picture_url, :width => 600) if @poste.picture.present? %

:picture

Page 20: Ruby On Rails Starter Kit

+2 XPNew level has been unlocked

Page 21: Ruby On Rails Starter Kit

Level #5

Create Static Pages

Page 22: Ruby On Rails Starter Kit

score: 000008 XP

If you want to create a static page you can do it by creating a controller with the corresponding view

$ rails generate controller pages about

Now you can visit your page through

$ http://localhost:3000/pages/about

Open up the view file corresponding to the pages#aboutcontroller and add some info there

app/views/pages/about.html.erb

Page 23: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 24: Ruby On Rails Starter Kit

Level #6

Bling Bling Baby

Page 25: Ruby On Rails Starter Kit

score: 000008 XP

Its all up to you to tweek your css file, wich is located in the following path:

app/assets/stylesheets/application.css

If you are planning to use bootstrap so make sure you add bootstrap to your Gemfile

gem 'bootstrap-sass', '~> 3.3.6'

Now Import bootstarp to your main application cssapp/assets/stylesheets/application.scss

@import "bootstrap-sprockets";@import "bootstrap";

Page 26: Ruby On Rails Starter Kit

+1 XPNew level has been unlocked

Page 27: Ruby On Rails Starter Kit

Level #7

Make some serious relations

Page 28: Ruby On Rails Starter Kit

score: 000009 XP

Create a comment scaffold, with the commentator name, the comment body (contents of the comment) and with the reference to the ideas table (post_id).

$ rails g scaffold comment user_name:string body:text post_id:integer

Then migrate your datebase:

rake db:migrate

Openup poste model File app/models/post.rb and add

has_many :comments

Page 29: Ruby On Rails Starter Kit

score: 000009 XP

And from the comment side add

belongs_to :post

And from the comment side add

<h3>Comments</h3><% @comments.each do |comment| %><div>

<strong><%= comment.user_name %></strong><br /><p><%= comment.body %></p><p><%= link_to 'Delete', comment_path(comment), method: :delete, data: { confirm: 'Are you

sure?' } %></p></div>

<% end %><%= render 'comments/form' %>

Page 30: Ruby On Rails Starter Kit

score: 000009 XP

Lets modifier the app/controllers/ideas_controller.rb to get fit with what we did in the previously changes

@comments = @idea.comments.all@comment = @idea.comments.build

And also add the following line app/views/comments/_form.html.erb

<%= f.hidden_field :idea_id %>

Page 31: Ruby On Rails Starter Kit

Congratulation!! you finished all the levels

of the gameWith 10 XP

Page 32: Ruby On Rails Starter Kit

Now you are ready for another levels of the game but you need extra life points !!

Page 33: Ruby On Rails Starter Kit

So follow us onFacebook Group Rails on Algeria

FB: El Orabi Mohamed Ikbal [email protected]