rails course day 5

13
Ruby on Rails Course Day 5 By @AlSayedGamal

Upload: al-sayed-gamal

Post on 22-Jan-2017

180 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Rails course day 5

Ruby on Rails Course Day 5

By @AlSayedGamal

Page 2: Rails course day 5

Views Haml and bootstrap

Page 3: Rails course day 5

Agenda

Views, partials and helpers

rendering and layout

adding haml and syntax difference compared to ERB

adding twitter bootstrap and responsive grid design basics

Page 4: Rails course day 5

Views, partials and helpers

Views are loaded by convention.

Normal content will fall in main yield part.

_ prefixed files are called partials and are rendered inside views for reusability sake.

Example: _form in any scaffold.

Page 5: Rails course day 5

Rendering and layout

Each controller is associated with layout.

And each layout has some regionsrepresented by yield statement and region name

Page 6: Rails course day 5

Helpers

Helper example:

form_for

link_to

You can define your own on app/helpers folder.

Page 7: Rails course day 5

Haml is DRY markdowncleaner views in action

Page 8: Rails course day 5

Haml Simple rules

%tagname#id.class

Page 9: Rails course day 5

Twitter BootstrapResponsive and grid design in action

Page 10: Rails course day 5

Installation and Usage

add following gems to your Gemfile and bundle

gem 'twitter-bootstrap-rails'

gem 'haml'

gem ‘haml-rails'

now install bootstrap static assets

rails generate bootstrap:install static

Generate fluid layout

rails g bootstrap:layout application fluid*

Scaffold and style

rails g bootstrap:themed <scaffold_model>s

*remove the .erb default one

Page 11: Rails course day 5

Add new regions to layout

-if content_for?

= yield = yield(:region)

= content_for do

Page 12: Rails course day 5

Associated Model form best practicesUse”accepts_nested_attributes_for” to accept data from associated model.

Use“fields_for” to show this associated model fields.

Use ”validated_associated” to validate related model

Useform builder’s collection_* to link related modelsExample: collection_select

Page 13: Rails course day 5

Exercise