SlideShare uma empresa Scribd logo
1 de 54
Baixar para ler offline
Did your Homework?
                    • Or promise you will
                      • (Chapters 8 & 9)
                      • Grab A Gowalla Tee Shirt!!
                      • Sizes run a bit small



@Schneems
Thursday, June 30, 2011
Beginner to Builder
                          Week 4
                          Richard Schneeman
                          @schneems




June, 2011
Thursday, June 30, 2011
Rails Hotline
                    • Call them and ask rails questions
                      • (877)- 817- 4190
                      • Free




@Schneems
Thursday, June 30, 2011
Rails - Week4
                          • Html, Javascript and CSS
                          • Error Handling
                            • begin - rescue - end
                          • Ajax - “Web 2.0”
                            • What is it?
                            • Partials
                            • Considerations
@Schneems
Thursday, June 30, 2011
HTML
                      • Hyper Text Markup Language
                      • Used to create Webpages




@Schneems
Thursday, June 30, 2011
HTML
               <div id="content" class="clearfix">

                          <h1>Keep up with your friends, share the places you go,<br /> and discover the
              extraordinary in the world around you.</h1>

                    <div id="content">
                        <div id="leather" class="clearfix">
                      <!-- tour -->
                        <div id="tour">

                          <div id="tour_inner" style="left: 0px; -webkit-transform: translate3d(0px, 0px, 0);">

                          <div id="homepage" style="background: transparent url(/images/tour/home-2.png) top
              left no-repeat;">
                                  <div class="feature friends">
                                      <div class="feature-icon"></div>
                                      <p><strong>Keep up with friends on your phone.</strong> Connect with
              friends from Facebook and Twitter to share where you're going.</p>
                                  </div>
                                  <div class="feature discover">
                                      <div class="feature-icon"></div>
                                      <p><strong>Discover new places and hotspots</strong> when you go out, then
              share your photos, recommendations and trips with friends!</p>
                                  </div>
                                  <div class="feature rewards">
                                      <div class="feature-icon"></div>
                                      <p><strong>Find inspiration to explore</strong> the world around you while
              picking up rewards from local eateries, venues and retail stores.</p>
                                  </div>
                              </div>

@Schneems
Thursday, June 30, 2011
HTML




@Schneems
Thursday, June 30, 2011
HTML elements
                      • Most elements have start and end
                        tags
                      • can have optional attributes
                          <p>This is an element</p>

                          <br /><!-- so is this -->

                          <h1 class=”foo”>
                            This is an element with an attribute
                          </h1>


@Schneems
Thursday, June 30, 2011
.html.erb
                      • ERB
                       • Embedded Ruby
                      • Ruby code that generates HTML
    <h1>People</h1>                                <h1>People</h1>
     <ul>                                          <ul>

                                              =>
       <% @people.each do |person| %>                <li>Bob</li>
          <li><%= person.first_name %></li>          <li>Joe</li>
       <% end -%>                                    <li>Mary</li>
     </ul>                                         </ul>

@Schneems
Thursday, June 30, 2011
.html.erb
                 <%= "This is an ERB Display Tag " %>

                 <% foo = "Embedded expression does not render" %>

                 <% 3.times do |i| %>
                   <%= "Hello #{i}" %>
                 <% end %>




@Schneems
Thursday, June 30, 2011
.html.haml
                      • ERB alternative
                      • Meaningful whitespace
                      • no “end” keyword
         %h1                                    <h1>People</h1>
           People                               <ul>
           %ul
             - @people.each do |person|    =>     <li>Bob</li>
                                                  <li>Joe</li>
               %li=person.first_name              <li>Mary</li>
                                                </ul>


@Schneems
Thursday, June 30, 2011
CSS
                      • Cascading Style Sheets
                       • Rules Based Style
                      • Casscade
                       • What happens if more than one rule
                         is applied?




@Schneems
Thursday, June 30, 2011
CSS
                      • ( # ) id
                      • ( . ) class
                      • element
                          // id selector
                            #main { background-color: orange;}
                          // class selector
                            .sidebar { color: black; }
                          // element selector
                            span { font-size: 24px;}
                          // mixed
                            span.sidebar { color: #C5C5C5; }
@Schneems
Thursday, June 30, 2011
CSS
                      • Inline styles
                      • Embedded style
                      • External style




@Schneems
Thursday, June 30, 2011
CSS
                          <!-- inline -->
                            <p style="color:black">tag</p>
                          <!-- Embedded -->
                            <style type="text/css">
                              p {color:black}
                            </style>
                          <!-- External -->
                            <link rel="stylesheet"
                              type="text/css" href="mystyle.css" />




@Schneems
Thursday, June 30, 2011
CSS
                      • Inheritance
                      • specificity
                      • location




@Schneems
Thursday, June 30, 2011
CSS
                      • Inheritence
                          p {
                                color: blue;
                          }


                          <p>
                            <span>Hello There</span>
                          </p>




@Schneems
Thursday, June 30, 2011
CSS
                      • Specificity
  *             {}           /*   a=0   b=0   c=0   d=0   ->   specificity   =   0,0,0,0   */
  li            {}           /*   a=0   b=0   c=0   d=1   ->   specificity   =   0,0,0,1   */
  li:first-line {}           /*   a=0   b=0   c=0   d=2   ->   specificity   =   0,0,0,2   */
  ul li         {}           /*   a=0   b=0   c=0   d=2   ->   specificity   =   0,0,0,2   */
  ul ol+li      {}           /*   a=0   b=0   c=0   d=3   ->   specificity   =   0,0,0,3   */
  h1 + *[rel=up]{}           /*   a=0   b=0   c=1   d=1   ->   specificity   =   0,0,1,1   */
  ul ol li.red {}            /*   a=0   b=0   c=1   d=3   ->   specificity   =   0,0,1,3   */
  li.red.level {}            /*   a=0   b=0   c=2   d=1   ->   specificity   =   0,0,2,1   */
  #x34y         {}           /*   a=0   b=1   c=0   d=0   ->   specificity   =   0,1,0,0   */



@Schneems
Thursday, June 30, 2011
CSS
                      • Location
                       • Later rules are more important
                          p {
                                color: blue;
                          }

                          p {
                                color: red;
                          }

                                       paragraph will be red
@Schneems
Thursday, June 30, 2011
Browser Wars
                      •   Different browsers
                          •   same content
                              •   Different Results
                                  •   IE
                                  •   Safari
                                  •   Chrome
                                  •   FireFox
                                  •   etc.

@Schneems
Thursday, June 30, 2011
JavaScript
                      • Client Side Scripting Language
                       • (The web browser is the client)
                      • Make your websited more dynamic

                          var name = prompt("What is your name?");
                          alert("Welcome " + name);



          Java is to JavaScript as Car is to Carpet
@Schneems
Thursday, June 30, 2011
Javascript
                      • Can manipulate the DOM
                      • DOM
                        • Document Object Model




@Schneems
Thursday, June 30, 2011
DOM




@Schneems
                          Document Object Model
Thursday, June 30, 2011
JavaScript Libraries
                      • Written in JavaScript
                      • Makes writing JavaScript easier
                       • jQuery (rails >= 3.1)
                       • Prototype (rails < 3.1)



@Schneems
Thursday, June 30, 2011
jQuery
                      • The pre-packaged JavaScript library
                        with rails 3.1

                 // Javascript
                   window.document.body.style.display = 'none';
                 // jQuery
                   $('body').hide()




@Schneems
Thursday, June 30, 2011
jQuery in Rails 3
                      • Use jQuery gem

                          gem install "jquery-rails"
                          rails generate jquery:install




@Schneems
Thursday, June 30, 2011
JavaScript Debugging
                      • Firefox #=> FireBug
                      • Safari #=> Inspect Element
                      • Chrome #=> Inspect Element
                      • IE #=> Don’t ever use IE



@Schneems
Thursday, June 30, 2011
CSS Debugging
                      • Firefox #=> FireBug
                      • Safari #=> Inspect Element
                      • Chrome #=> Inspect Element
                      • IE #=> Don’t ever use IE



@Schneems
Thursday, June 30, 2011
Error Handling
                          • What is an Exception?
                          • How can we handle Exceptions
                          • Rails’ errors attributes




@Schneems
Thursday, June 30, 2011
Error Handling
                          • What is an Exception?
                           • A standard ruby object
                                   Create with raise

           raise “you entered an invalid attribute”

                           Optionally provide type of exception


            raise ArgumentError, "Argument is not numeric"




                                                                  rubylearning.org
@Schneems
Thursday, June 30, 2011
Raise
                      • Code confident
                       • raise errors in truly exceptional
                         situations
                       • error will be logged
                       • user will endis handled a 500 error
                         unless error
                                        up seeing


                          raise "bad stuff happened"



@Schneems
Thursday, June 30, 2011
Error Handling
                           • Begin-Rescue-End (Ruby)
                            • Like Try - Catch
                            • Begin attempts to run code
                            • If exception, then rescue catches
                              the error
                          begin
                            dept = Department.find(thisdept)
                          rescue
                            "No Department"
                          end


@Schneems
Thursday, June 30, 2011
Error Handling
                          • Begin-Rescue-End (Ruby)
                           • Ensure - Runs regardless of errors
                          begin
                            file = open("/tmp/some_file", "w")
                            # ... write to the file ...
                          rescue
                            # ... handle the exceptions ...
                          ensure
                            file.close   # ... and this always happens.
                          end


@Schneems
Thursday, June 30, 2011
Error Handling
                           • Begin-Rescue-End (Ruby)
                      rescue defaults to StandardError

                                        rescue

                    rescue can handle different types of exceptions

                                        rescue SyntaxError

                    rescue Exception will rescue any exception

                                        rescue Exception

@Schneems
Thursday, June 30, 2011
Error Handling
                          • Logger
                           • Rails logs activity
                             • {production, development, test}.log
                           • Useful to see where errors occured
                           • Use tail to view current activity
                          $: tail -f logs/production.log


                          (much better than trying to load a +200mb log file into a text editor)


@Schneems
Thursday, June 30, 2011
Error Handling
                          • Logger
                           • Specify different levels of logging
                             details
                          config/environments/production.rb
                           config.log_level = :debug

                            • Filter out sensitive info from log
                          config/environment.rb
                           config.filter_parameters += [:password]

                                 Now, no passwords will show up in any log file

                               Keeps hackers in the dark, and SysAdmins honest



@Schneems
Thursday, June 30, 2011
Error Handling
                          • Logger
                           • Different log levels filtered by logger
                           Example:                                Levels




                                config/environments/production.rb

                                 config.log_level = :debug

@Schneems
Thursday, June 30, 2011
Error Handling
                          • Logger
                           • How do I write something to the log?
                             • Errors and exceptions added
                             • call logger directly
                          logger.error "Something Bad Happened"

                           Puts info in log without raising error

@Schneems
Thursday, June 30, 2011
Errors in Rails
                           • Errors in rails
                            • Attached to objects
                            • Access through errors method
                              • @dog.errors
                          <% @dog.errors.full_messages.each do |msg| %>
                            <li><%=raw msg %></li>
                          <% end %>



@Schneems
Thursday, June 30, 2011
Errors in Rails
                          • How do we add Errors to objects?
                           • Pre-Built Validations
     validates :name, :presence => true, :uniqueness => true

                           • Custom Validations
     validate :speed_must_be_divisible_by_three
     private
     def speed_must_be_divisible_by_three
         errors.add(:speed, "Speed is not divisible by three") unless speed%3 == 0
     end



@Schneems
Thursday, June 30, 2011
Ajax
               • What is Ajax
                  • Asynchronous Javascript
                  • Doesn't require page reload



           Maps.google.com             Gowalla.com
@Schneems
Thursday, June 30, 2011
Ajax
  • Example:
        1. Send form data to Controller via JS
        2. Controller parses form data
        3. Controller returns info to browser
        4. browser renders info
        5. user sees no page reload
        6. magic!!

@Schneems
Thursday, June 30, 2011
Ajax
               • Considerations
                 • User Expectations
                   • Loading...
                   • Don’t break the back button
                   • No Javascript?
                   • Principle of least surprise

@Schneems
Thursday, June 30, 2011
Ajax
               • Loading
                  • Usually Gif Files
                  • Doesn't require page reload
                  • Lets user know “something” is
                    happening
                                         Loading...




@Schneems
Thursday, June 30, 2011
Ajax
               • Loading
                  • Usually Gif Files
                  • Doesn't require page reload
                  • Lets user know “something” is
                    happening
                                         Loading...




@Schneems
Thursday, June 30, 2011
Ajax
               • Don’t break the back button
                   • Don’t do it
               • Don’t break user copied urls
                   • Don’t do it
               • If you’re doing it
                   • Don’t (and find a fix)

@Schneems
Thursday, June 30, 2011
Ajax
               • What if I’m using IE 5?
                 • Unobtrusive Javascript
                   • No reduced Functionality
                   • Rails 3 Does it



@Schneems
Thursday, June 30, 2011
Ajax - In Rails
         • Unobtrusive Javascript
     <% form_tag search_path, :id => "search-form", :remote => true do %>
       <span>Username: </span>
       <%= text_field_tag 'username' %>
       <%= submit_tag 'Submit'%>
     <% end %>

              If no Javascript We Still use Normal Http Request
                   <form action="/spot/show" data-remote="true" id="search-form" method="post">
                       <div>
                          <input name="authenticity_token" type="hidden" value="7+2/AAuZF1X55xdRW0dHtx/
                   7NL6aq8stuhqeQFeaSfI=" />
                          Code Here
                          </div>
                          <span>Username: </span>
                     <input id="username" name="username" type="text" />
                     <input name="commit" type="submit" value="Submit" />
                          </form>
                   </div>


@Schneems
Thursday, June 30, 2011
Ajax - In Rails
               • Okay, so I asynchronously submit stuff, what do
                      I do when I get a response?
                     • Handle No response case (404,500, etc.)
                     • Render a format
                      • Controller responds to different formats
     respond_to do |format|
       format.html { redirect_to(person_list_url) }
       format.json
       format.xml { render :xml => @person.to_xml(:include => @company) }
     end

@Schneems
Thursday, June 30, 2011
Ajax - In Rails
               • Different formats
                • Render Different results
                          render   :xml
                          render   :json
                          render   :text => “hey there”
                          render   :js => “alert(‘hello’);”
                          render   :template => “dogs/show”
                          render   :partial => “form”


                                              Partials?
@Schneems
Thursday, June 30, 2011
Ajax - In Rails
              • Partials
               • Reusable html.erb templates
                          app/views/dogs/_form.html.erb   <%= form_for(@dog) do |f| %>
                                                            <% if @dog.errors.any? %>
                                                          ...
                                                                  <% end %>
                                                            <% end %>
                                                            <div class="field">
                                                               <%= f.label :name %><br />
                                                               <%= f.text_field :name %>
                                                            </div>
                                                              ...
                                                            <div class="actions">
                            Code Here                          <%= f.submit %>
                                                            </div>
                                                          <% end %>


@Schneems
Thursday, June 30, 2011
Ajax - In Rails
              • Partials - Stay DRY
               • Put partials into your code
                          app/views/dogs/new.html.erb
                            <h1>New dog</h1>
                            <%= render 'form' %>
                            <%= link_to 'Back', dogs_path %>

                    • Render partial from controller
                          app/controllers/dogs.rb
                           def new
                             @dog = Dog.new
                             respond_to do |format|
                               format.html # new.html.erb
                               format.js {render :partial => "form"}
                               format.xml { render :xml => @dog }
                             end
                           end
@Schneems
Thursday, June 30, 2011
Ajax
               • Don’t go overboard
                 • Does it truly add to the functionality of
                   the website?
                 • Does it take away anything from the
                   website?
                 • What can we assume about the user?
                 • When in doubt, don’t use it

@Schneems
Thursday, June 30, 2011
Questions?
                  Assignment for Next Week: Chapters 10, 11

                          http://guides.rubyonrails.org
                          http://stackoverflow.com

@Schneems
Thursday, June 30, 2011

Mais conteúdo relacionado

Semelhante a Rails 3 Beginner to Builder 2011 Week 4

Darwin web standards
Darwin web standardsDarwin web standards
Darwin web standards
Justin Avery
 
Basics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPointBasics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPoint
Sahil Gandhi
 
Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSS
Jussi Pohjolainen
 
LIS3353 SP12 Week 4
LIS3353 SP12 Week 4LIS3353 SP12 Week 4
LIS3353 SP12 Week 4
Amanda Case
 
Be nice to your designers
Be nice to your designersBe nice to your designers
Be nice to your designers
Pai-Cheng Tao
 
Intro to jQuery for Drupal
Intro to jQuery for DrupalIntro to jQuery for Drupal
Intro to jQuery for Drupal
jhamiltoorion
 

Semelhante a Rails 3 Beginner to Builder 2011 Week 4 (20)

Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSS
 
BITM3730 9-27.pptx
BITM3730 9-27.pptxBITM3730 9-27.pptx
BITM3730 9-27.pptx
 
CSS
CSSCSS
CSS
 
Darwin web standards
Darwin web standardsDarwin web standards
Darwin web standards
 
Basics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPointBasics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPoint
 
HTML5 - getting started
HTML5 - getting startedHTML5 - getting started
HTML5 - getting started
 
HTML/CSS Workshop @ Searchcamp
HTML/CSS Workshop @ SearchcampHTML/CSS Workshop @ Searchcamp
HTML/CSS Workshop @ Searchcamp
 
Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSS
 
LIS3353 SP12 Week 4
LIS3353 SP12 Week 4LIS3353 SP12 Week 4
LIS3353 SP12 Week 4
 
Artdm171 Week4 Tags
Artdm171 Week4 TagsArtdm171 Week4 Tags
Artdm171 Week4 Tags
 
Modular HTML & CSS
Modular HTML & CSSModular HTML & CSS
Modular HTML & CSS
 
HTML Lecture Part 1 of 2
HTML Lecture Part 1 of 2HTML Lecture Part 1 of 2
HTML Lecture Part 1 of 2
 
Web 101 intro to html
Web 101  intro to htmlWeb 101  intro to html
Web 101 intro to html
 
Css
CssCss
Css
 
Intro to HTML 5 / CSS 3
Intro to HTML 5 / CSS 3Intro to HTML 5 / CSS 3
Intro to HTML 5 / CSS 3
 
Lecture 2 - HTML Basics
Lecture 2 - HTML BasicsLecture 2 - HTML Basics
Lecture 2 - HTML Basics
 
Object Oriented CSS - Joomla!dagen Nederland 2014
Object Oriented CSS - Joomla!dagen Nederland 2014Object Oriented CSS - Joomla!dagen Nederland 2014
Object Oriented CSS - Joomla!dagen Nederland 2014
 
Be nice to your designers
Be nice to your designersBe nice to your designers
Be nice to your designers
 
css v1 guru
css v1 gurucss v1 guru
css v1 guru
 
Intro to jQuery for Drupal
Intro to jQuery for DrupalIntro to jQuery for Drupal
Intro to jQuery for Drupal
 

Mais de Richard Schneeman

Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6
Richard Schneeman
 

Mais de Richard Schneeman (9)

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQL
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8
 
Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6
 
Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6
 
Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5
 
UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 

Último

KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
Cara Menggugurkan Kandungan 087776558899
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
vikas rana
 
the Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentationthe Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentation
brynpueblos04
 

Último (14)

2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
 
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
 
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
 
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
 
LC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfLC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdf
 
WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptx
 
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
 
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
 
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theory
 
the Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentationthe Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentation
 
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
 

Rails 3 Beginner to Builder 2011 Week 4

  • 1. Did your Homework? • Or promise you will • (Chapters 8 & 9) • Grab A Gowalla Tee Shirt!! • Sizes run a bit small @Schneems Thursday, June 30, 2011
  • 2. Beginner to Builder Week 4 Richard Schneeman @schneems June, 2011 Thursday, June 30, 2011
  • 3. Rails Hotline • Call them and ask rails questions • (877)- 817- 4190 • Free @Schneems Thursday, June 30, 2011
  • 4. Rails - Week4 • Html, Javascript and CSS • Error Handling • begin - rescue - end • Ajax - “Web 2.0” • What is it? • Partials • Considerations @Schneems Thursday, June 30, 2011
  • 5. HTML • Hyper Text Markup Language • Used to create Webpages @Schneems Thursday, June 30, 2011
  • 6. HTML <div id="content" class="clearfix"> <h1>Keep up with your friends, share the places you go,<br /> and discover the extraordinary in the world around you.</h1> <div id="content"> <div id="leather" class="clearfix"> <!-- tour --> <div id="tour"> <div id="tour_inner" style="left: 0px; -webkit-transform: translate3d(0px, 0px, 0);"> <div id="homepage" style="background: transparent url(/images/tour/home-2.png) top left no-repeat;"> <div class="feature friends"> <div class="feature-icon"></div> <p><strong>Keep up with friends on your phone.</strong> Connect with friends from Facebook and Twitter to share where you're going.</p> </div> <div class="feature discover"> <div class="feature-icon"></div> <p><strong>Discover new places and hotspots</strong> when you go out, then share your photos, recommendations and trips with friends!</p> </div> <div class="feature rewards"> <div class="feature-icon"></div> <p><strong>Find inspiration to explore</strong> the world around you while picking up rewards from local eateries, venues and retail stores.</p> </div> </div> @Schneems Thursday, June 30, 2011
  • 8. HTML elements • Most elements have start and end tags • can have optional attributes <p>This is an element</p> <br /><!-- so is this --> <h1 class=”foo”> This is an element with an attribute </h1> @Schneems Thursday, June 30, 2011
  • 9. .html.erb • ERB • Embedded Ruby • Ruby code that generates HTML <h1>People</h1> <h1>People</h1> <ul> <ul> => <% @people.each do |person| %> <li>Bob</li> <li><%= person.first_name %></li> <li>Joe</li> <% end -%> <li>Mary</li> </ul> </ul> @Schneems Thursday, June 30, 2011
  • 10. .html.erb <%= "This is an ERB Display Tag " %> <% foo = "Embedded expression does not render" %> <% 3.times do |i| %> <%= "Hello #{i}" %> <% end %> @Schneems Thursday, June 30, 2011
  • 11. .html.haml • ERB alternative • Meaningful whitespace • no “end” keyword %h1 <h1>People</h1> People <ul> %ul - @people.each do |person| => <li>Bob</li> <li>Joe</li> %li=person.first_name <li>Mary</li> </ul> @Schneems Thursday, June 30, 2011
  • 12. CSS • Cascading Style Sheets • Rules Based Style • Casscade • What happens if more than one rule is applied? @Schneems Thursday, June 30, 2011
  • 13. CSS • ( # ) id • ( . ) class • element // id selector #main { background-color: orange;} // class selector .sidebar { color: black; } // element selector span { font-size: 24px;} // mixed span.sidebar { color: #C5C5C5; } @Schneems Thursday, June 30, 2011
  • 14. CSS • Inline styles • Embedded style • External style @Schneems Thursday, June 30, 2011
  • 15. CSS <!-- inline --> <p style="color:black">tag</p> <!-- Embedded --> <style type="text/css"> p {color:black} </style> <!-- External --> <link rel="stylesheet" type="text/css" href="mystyle.css" /> @Schneems Thursday, June 30, 2011
  • 16. CSS • Inheritance • specificity • location @Schneems Thursday, June 30, 2011
  • 17. CSS • Inheritence p { color: blue; } <p> <span>Hello There</span> </p> @Schneems Thursday, June 30, 2011
  • 18. CSS • Specificity * {} /* a=0 b=0 c=0 d=0 -> specificity = 0,0,0,0 */ li {} /* a=0 b=0 c=0 d=1 -> specificity = 0,0,0,1 */ li:first-line {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */ ul li {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */ ul ol+li {} /* a=0 b=0 c=0 d=3 -> specificity = 0,0,0,3 */ h1 + *[rel=up]{} /* a=0 b=0 c=1 d=1 -> specificity = 0,0,1,1 */ ul ol li.red {} /* a=0 b=0 c=1 d=3 -> specificity = 0,0,1,3 */ li.red.level {} /* a=0 b=0 c=2 d=1 -> specificity = 0,0,2,1 */ #x34y {} /* a=0 b=1 c=0 d=0 -> specificity = 0,1,0,0 */ @Schneems Thursday, June 30, 2011
  • 19. CSS • Location • Later rules are more important p { color: blue; } p { color: red; } paragraph will be red @Schneems Thursday, June 30, 2011
  • 20. Browser Wars • Different browsers • same content • Different Results • IE • Safari • Chrome • FireFox • etc. @Schneems Thursday, June 30, 2011
  • 21. JavaScript • Client Side Scripting Language • (The web browser is the client) • Make your websited more dynamic var name = prompt("What is your name?"); alert("Welcome " + name); Java is to JavaScript as Car is to Carpet @Schneems Thursday, June 30, 2011
  • 22. Javascript • Can manipulate the DOM • DOM • Document Object Model @Schneems Thursday, June 30, 2011
  • 23. DOM @Schneems Document Object Model Thursday, June 30, 2011
  • 24. JavaScript Libraries • Written in JavaScript • Makes writing JavaScript easier • jQuery (rails >= 3.1) • Prototype (rails < 3.1) @Schneems Thursday, June 30, 2011
  • 25. jQuery • The pre-packaged JavaScript library with rails 3.1 // Javascript window.document.body.style.display = 'none'; // jQuery $('body').hide() @Schneems Thursday, June 30, 2011
  • 26. jQuery in Rails 3 • Use jQuery gem gem install "jquery-rails" rails generate jquery:install @Schneems Thursday, June 30, 2011
  • 27. JavaScript Debugging • Firefox #=> FireBug • Safari #=> Inspect Element • Chrome #=> Inspect Element • IE #=> Don’t ever use IE @Schneems Thursday, June 30, 2011
  • 28. CSS Debugging • Firefox #=> FireBug • Safari #=> Inspect Element • Chrome #=> Inspect Element • IE #=> Don’t ever use IE @Schneems Thursday, June 30, 2011
  • 29. Error Handling • What is an Exception? • How can we handle Exceptions • Rails’ errors attributes @Schneems Thursday, June 30, 2011
  • 30. Error Handling • What is an Exception? • A standard ruby object Create with raise raise “you entered an invalid attribute” Optionally provide type of exception raise ArgumentError, "Argument is not numeric" rubylearning.org @Schneems Thursday, June 30, 2011
  • 31. Raise • Code confident • raise errors in truly exceptional situations • error will be logged • user will endis handled a 500 error unless error up seeing raise "bad stuff happened" @Schneems Thursday, June 30, 2011
  • 32. Error Handling • Begin-Rescue-End (Ruby) • Like Try - Catch • Begin attempts to run code • If exception, then rescue catches the error begin dept = Department.find(thisdept) rescue "No Department" end @Schneems Thursday, June 30, 2011
  • 33. Error Handling • Begin-Rescue-End (Ruby) • Ensure - Runs regardless of errors begin file = open("/tmp/some_file", "w") # ... write to the file ... rescue # ... handle the exceptions ... ensure file.close # ... and this always happens. end @Schneems Thursday, June 30, 2011
  • 34. Error Handling • Begin-Rescue-End (Ruby) rescue defaults to StandardError rescue rescue can handle different types of exceptions rescue SyntaxError rescue Exception will rescue any exception rescue Exception @Schneems Thursday, June 30, 2011
  • 35. Error Handling • Logger • Rails logs activity • {production, development, test}.log • Useful to see where errors occured • Use tail to view current activity $: tail -f logs/production.log (much better than trying to load a +200mb log file into a text editor) @Schneems Thursday, June 30, 2011
  • 36. Error Handling • Logger • Specify different levels of logging details config/environments/production.rb config.log_level = :debug • Filter out sensitive info from log config/environment.rb config.filter_parameters += [:password] Now, no passwords will show up in any log file Keeps hackers in the dark, and SysAdmins honest @Schneems Thursday, June 30, 2011
  • 37. Error Handling • Logger • Different log levels filtered by logger Example: Levels config/environments/production.rb config.log_level = :debug @Schneems Thursday, June 30, 2011
  • 38. Error Handling • Logger • How do I write something to the log? • Errors and exceptions added • call logger directly logger.error "Something Bad Happened" Puts info in log without raising error @Schneems Thursday, June 30, 2011
  • 39. Errors in Rails • Errors in rails • Attached to objects • Access through errors method • @dog.errors <% @dog.errors.full_messages.each do |msg| %> <li><%=raw msg %></li> <% end %> @Schneems Thursday, June 30, 2011
  • 40. Errors in Rails • How do we add Errors to objects? • Pre-Built Validations validates :name, :presence => true, :uniqueness => true • Custom Validations validate :speed_must_be_divisible_by_three private def speed_must_be_divisible_by_three errors.add(:speed, "Speed is not divisible by three") unless speed%3 == 0 end @Schneems Thursday, June 30, 2011
  • 41. Ajax • What is Ajax • Asynchronous Javascript • Doesn't require page reload Maps.google.com Gowalla.com @Schneems Thursday, June 30, 2011
  • 42. Ajax • Example: 1. Send form data to Controller via JS 2. Controller parses form data 3. Controller returns info to browser 4. browser renders info 5. user sees no page reload 6. magic!! @Schneems Thursday, June 30, 2011
  • 43. Ajax • Considerations • User Expectations • Loading... • Don’t break the back button • No Javascript? • Principle of least surprise @Schneems Thursday, June 30, 2011
  • 44. Ajax • Loading • Usually Gif Files • Doesn't require page reload • Lets user know “something” is happening Loading... @Schneems Thursday, June 30, 2011
  • 45. Ajax • Loading • Usually Gif Files • Doesn't require page reload • Lets user know “something” is happening Loading... @Schneems Thursday, June 30, 2011
  • 46. Ajax • Don’t break the back button • Don’t do it • Don’t break user copied urls • Don’t do it • If you’re doing it • Don’t (and find a fix) @Schneems Thursday, June 30, 2011
  • 47. Ajax • What if I’m using IE 5? • Unobtrusive Javascript • No reduced Functionality • Rails 3 Does it @Schneems Thursday, June 30, 2011
  • 48. Ajax - In Rails • Unobtrusive Javascript <% form_tag search_path, :id => "search-form", :remote => true do %> <span>Username: </span> <%= text_field_tag 'username' %> <%= submit_tag 'Submit'%> <% end %> If no Javascript We Still use Normal Http Request <form action="/spot/show" data-remote="true" id="search-form" method="post"> <div> <input name="authenticity_token" type="hidden" value="7+2/AAuZF1X55xdRW0dHtx/ 7NL6aq8stuhqeQFeaSfI=" /> Code Here </div> <span>Username: </span> <input id="username" name="username" type="text" /> <input name="commit" type="submit" value="Submit" /> </form> </div> @Schneems Thursday, June 30, 2011
  • 49. Ajax - In Rails • Okay, so I asynchronously submit stuff, what do I do when I get a response? • Handle No response case (404,500, etc.) • Render a format • Controller responds to different formats respond_to do |format| format.html { redirect_to(person_list_url) } format.json format.xml { render :xml => @person.to_xml(:include => @company) } end @Schneems Thursday, June 30, 2011
  • 50. Ajax - In Rails • Different formats • Render Different results render :xml render :json render :text => “hey there” render :js => “alert(‘hello’);” render :template => “dogs/show” render :partial => “form” Partials? @Schneems Thursday, June 30, 2011
  • 51. Ajax - In Rails • Partials • Reusable html.erb templates app/views/dogs/_form.html.erb <%= form_for(@dog) do |f| %> <% if @dog.errors.any? %> ... <% end %> <% end %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> ... <div class="actions"> Code Here <%= f.submit %> </div> <% end %> @Schneems Thursday, June 30, 2011
  • 52. Ajax - In Rails • Partials - Stay DRY • Put partials into your code app/views/dogs/new.html.erb <h1>New dog</h1> <%= render 'form' %> <%= link_to 'Back', dogs_path %> • Render partial from controller app/controllers/dogs.rb def new @dog = Dog.new respond_to do |format| format.html # new.html.erb format.js {render :partial => "form"} format.xml { render :xml => @dog } end end @Schneems Thursday, June 30, 2011
  • 53. Ajax • Don’t go overboard • Does it truly add to the functionality of the website? • Does it take away anything from the website? • What can we assume about the user? • When in doubt, don’t use it @Schneems Thursday, June 30, 2011
  • 54. Questions? Assignment for Next Week: Chapters 10, 11 http://guides.rubyonrails.org http://stackoverflow.com @Schneems Thursday, June 30, 2011