SlideShare ist ein Scribd-Unternehmen logo
1 von 32
More to RoC Weibo
Login/Logout, Sign Up
$ rails g resource user email:string name:string password:string
$ rails g controller sessions
Model
        User (email, name, password...)



Controller                      Controller
SessionsCotnroller               UsersController
  (Login/Logout)                    (Sign Up)
Model
      app/models/user.rb
class User < ActiveRecord::Base
  validates_presence_of :email, :password
  validates_confirmation_of :password

  has_many :messages
  has_many :replies
end
Migration
db/migrate/yyyyMMddhhmmss_create_users.rb

       class CreateUsers < ActiveRecord::Migration
         def self.up
           create_table :users do |t|
             t.string :email
             t.string :name
             t.string :password

             t.timestamps
           end
         end

         def self.down
           drop_table :users
         end
       end
Sign Up
app/controllers/users_controller.rb
 class UsersController < ApplicationController
   def new # show the sign up page
   end

   def create # register a new user
   end
 end
Login/Logout
app/controllers/sessions_controller.rb
 class SessionsController < ApplicationController
   def destroy # logout
   end

   def create # login
   end
 end
A little about RESTful
     create v.s. register/login
        destroy v.s. logout
• Controller       Resource


• Resource operations:
 • Create, Read, Update, Delete
• Only 7 actions:
 • index, new, create (C), edit, update (U),
    show (R), destroy (D)
Login    Create a Session

Logout    Destroy a Session

Sign Up    Create a User
To be RESTFul,   (controllers)
http://www.slideshare.net/shaokun/rest-ruby-on-rails
Migration
$ rails g migration add_user_id_to_messages_replies
$ rake db:migrate
class AddUserIdToMessagesReplies < ActiveRecord::Migration
  def self.up
    add_column :messages, :user_id, :integer
    add_column :replies, :user_id, :integer
  end

  def self.down
    remove_column :messages, :user_id
    remove_column :replies, :user_id
  end
end
Validations
class User < ActiveRecord::Base
  validates_presence_of :email, :password
  validates_confirmation_of :password,
                            :message => "should match confirmation"
end




    <%= password_field "user", "password" %>
    <%= password_field "user", "password_confirmation" %>
class User < ActiveRecord::Base
  validates_exclusion_of :age,
    :in => 30..60, :message => "This site is only for under 30 and over 60"
  validates_format_of :email,
    :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})Z/i
end
•   http://api.rubyonrails.org/

    •   validates_acceptance_of

    •   validates_confirmation_of

    •   validates_exclusion_of

    •   validates_format_of

    •   validates_inclusion_of

    •   validates_length_of

    •   validates_numericality_of

    •   validates_presence_of
AJAX
View
               <%= form_for(@message) do |f| %>
                 ...
               <% end %>




       <%= form_for(@message, :remote => true) do |f| %>
         ...
       <% end %>
Controller


             def create
               respond_to do |format|
                 format.html { redirect_to(messages_url) }
                 format.js {
                   render :update do |page|
                     ...
                   end
                 }
               end
             end
Functional/Unit Test
•   $ rake db:test:clone_structure


•   $ rake test


•   $ ruby -Itest test/unit/user_test.rb


•   $ ruby -Itest test/unit/user_test.rb -n TEST_CASE_METHOD_NAME
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  test "should create a new user" do
    assert_difference("User.count", +1) do
      post :create, :user => {
        :email => "shaokun.wu@gmail.com",
        :password => "a"
      }

      assert_redirected_to root_path
    end
  end
end
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "should set the name through email" do
    user = User.new(:email => "shaokun.wu@gmail.com", :password => "a")
    assert user.save
    assert_equal "shaokun.wu", user.name
  end
end
Deploy
    App
• PHP/ASP          Upload


 • 200     /


 •        Sina App Engine


• Rails
 • VPS: linode.com, AWS EC2
Heroku
http://heroku.com
•   $ gem install heroku

•   $ heroku create roc-demo2

•   $ heroku keys:add

•   $ git remote add heroku git@heroku.com:roc-demo2.git

•   $ git push heroku master

•   $ heroku rake db:migrate
C:Sitesroc-demo2>git push heroku master
Counting objects: 313, done.
Compressing objects: 100% (185/185), done.
Writing objects: 100% (313/313), 7.74 MiB | 15 KiB/s, done.
Total 313 (delta 123), reused 232 (delta 94)

-----> Heroku receiving push
-----> Rails app detected
-----> Detected Rails is not set to serve static_assets
       Installing rails3_serve_static_assets... done
-----> Configure Rails 3 to disable x-sendfile
       Installing rails3_disable_x_sendfile... done
-----> Configure Rails to log to stdout
       Installing rails_log_stdout... done
-----> Gemfile detected, running Bundler version 1.0.7
       Unresolved dependencies detected; Installing...
       Using --without development:test
       Fetching source index for http://rubygems.org/
       Installing rake (0.8.7)
       ...
       Your bundle is complete! It was installed into ./.bundle/gems/
-----> Compiled slug size is 3.7MB
-----> Launching... done, v4
       http://roc-demo2.heroku.com deployed to Heroku

To git@heroku.com:roc-demo2.git
 * [new branch]      master -> master
Let’s code...
• Get the code:
  http://github.com/kudelabs/roc-demo2

• See it in action:
  http://demo2.railsoncampus.com

• keep in touch:
  @railsoncampus
  http://weibo.com/1944841390

Weitere ähnliche Inhalte

Was ist angesagt?

Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and othersYusuke Wada
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Racksickill
 
Web service with Laravel
Web service with LaravelWeb service with Laravel
Web service with LaravelAbuzer Firdousi
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Prxibbar
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel PassportMichael Peacock
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortegaarman o
 
Wykorzystanie form request przy implementacji API w Laravelu
Wykorzystanie form request przy implementacji API w LaraveluWykorzystanie form request przy implementacji API w Laravelu
Wykorzystanie form request przy implementacji API w LaraveluLaravel Poland MeetUp
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroChristopher Pecoraro
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Sumy PHP User Grpoup
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutVic Metcalfe
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Sumy PHP User Grpoup
 

Was ist angesagt? (20)

Phinx talk
Phinx talkPhinx talk
Phinx talk
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Getting Started-with-Laravel
Getting Started-with-LaravelGetting Started-with-Laravel
Getting Started-with-Laravel
 
Fabric Python Lib
Fabric Python LibFabric Python Lib
Fabric Python Lib
 
Web service with Laravel
Web service with LaravelWeb service with Laravel
Web service with Laravel
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel Passport
 
Express JS
Express JSExpress JS
Express JS
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Wykorzystanie form request przy implementacji API w Laravelu
Wykorzystanie form request przy implementacji API w LaraveluWykorzystanie form request przy implementacji API w Laravelu
Wykorzystanie form request przy implementacji API w Laravelu
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and Knockout
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
 

Andere mochten auch

VIM for the PHP Developer
VIM for the PHP DeveloperVIM for the PHP Developer
VIM for the PHP DeveloperJohn Congdon
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Railsshaokun
 
WebSocket 实时推特流
WebSocket 实时推特流WebSocket 实时推特流
WebSocket 实时推特流shaokun
 
iOS 图片浏览器 DIY
iOS 图片浏览器 DIYiOS 图片浏览器 DIY
iOS 图片浏览器 DIYshaokun
 
Git flow
Git flowGit flow
Git flowshaokun
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
 
Namespace less engine
Namespace less engineNamespace less engine
Namespace less engineshaokun
 

Andere mochten auch (8)

VIM for the PHP Developer
VIM for the PHP DeveloperVIM for the PHP Developer
VIM for the PHP Developer
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Rails
 
WebSocket 实时推特流
WebSocket 实时推特流WebSocket 实时推特流
WebSocket 实时推特流
 
Rack
RackRack
Rack
 
iOS 图片浏览器 DIY
iOS 图片浏览器 DIYiOS 图片浏览器 DIY
iOS 图片浏览器 DIY
 
Git flow
Git flowGit flow
Git flow
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Namespace less engine
Namespace less engineNamespace less engine
Namespace less engine
 

Ähnlich wie More to RoC weibo

What's new in Rails 4
What's new in Rails 4What's new in Rails 4
What's new in Rails 4Fabio Akita
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxWen-Tien Chang
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Building Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelBuilding Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelpauldix
 
Building Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelBuilding Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelpauldix
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Plataformatec
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2pyjonromero
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4shnikola
 
Simplify Your Rails Controllers With a Vengeance
Simplify Your Rails Controllers With a VengeanceSimplify Your Rails Controllers With a Vengeance
Simplify Your Rails Controllers With a Vengeancebrianauton
 
Rails Routing and URL design
Rails Routing and URL designRails Routing and URL design
Rails Routing and URL designhiq5
 
Rails World 2023: Powerful Rails Features You Might Not Know
Rails World 2023: Powerful Rails Features You Might Not KnowRails World 2023: Powerful Rails Features You Might Not Know
Rails World 2023: Powerful Rails Features You Might Not KnowChris Oliver
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatternsChul Ju Hong
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2RORLAB
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-publicChul Ju Hong
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Joao Lucas Santana
 

Ähnlich wie More to RoC weibo (20)

What's new in Rails 4
What's new in Rails 4What's new in Rails 4
What's new in Rails 4
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 Ajax
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Building Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelBuilding Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModel
 
Building Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModelBuilding Web Service Clients with ActiveModel
Building Web Service Clients with ActiveModel
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
 
Simplify Your Rails Controllers With a Vengeance
Simplify Your Rails Controllers With a VengeanceSimplify Your Rails Controllers With a Vengeance
Simplify Your Rails Controllers With a Vengeance
 
Rails Routing and URL design
Rails Routing and URL designRails Routing and URL design
Rails Routing and URL design
 
Rails World 2023: Powerful Rails Features You Might Not Know
Rails World 2023: Powerful Rails Features You Might Not KnowRails World 2023: Powerful Rails Features You Might Not Know
Rails World 2023: Powerful Rails Features You Might Not Know
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)
 

More to RoC weibo

  • 1. More to RoC Weibo
  • 2. Login/Logout, Sign Up $ rails g resource user email:string name:string password:string $ rails g controller sessions
  • 3. Model User (email, name, password...) Controller Controller SessionsCotnroller UsersController (Login/Logout) (Sign Up)
  • 4. Model app/models/user.rb class User < ActiveRecord::Base validates_presence_of :email, :password validates_confirmation_of :password has_many :messages has_many :replies end
  • 5. Migration db/migrate/yyyyMMddhhmmss_create_users.rb class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :email t.string :name t.string :password t.timestamps end end def self.down drop_table :users end end
  • 6. Sign Up app/controllers/users_controller.rb class UsersController < ApplicationController def new # show the sign up page end def create # register a new user end end
  • 7. Login/Logout app/controllers/sessions_controller.rb class SessionsController < ApplicationController def destroy # logout end def create # login end end
  • 8. A little about RESTful create v.s. register/login destroy v.s. logout
  • 9. • Controller Resource • Resource operations: • Create, Read, Update, Delete • Only 7 actions: • index, new, create (C), edit, update (U), show (R), destroy (D)
  • 10. Login Create a Session Logout Destroy a Session Sign Up Create a User
  • 11. To be RESTFul, (controllers)
  • 13. Migration $ rails g migration add_user_id_to_messages_replies $ rake db:migrate
  • 14. class AddUserIdToMessagesReplies < ActiveRecord::Migration def self.up add_column :messages, :user_id, :integer add_column :replies, :user_id, :integer end def self.down remove_column :messages, :user_id remove_column :replies, :user_id end end
  • 16. class User < ActiveRecord::Base validates_presence_of :email, :password validates_confirmation_of :password, :message => "should match confirmation" end <%= password_field "user", "password" %> <%= password_field "user", "password_confirmation" %>
  • 17. class User < ActiveRecord::Base validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60" validates_format_of :email, :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})Z/i end
  • 18. http://api.rubyonrails.org/ • validates_acceptance_of • validates_confirmation_of • validates_exclusion_of • validates_format_of • validates_inclusion_of • validates_length_of • validates_numericality_of • validates_presence_of
  • 19. AJAX
  • 20. View <%= form_for(@message) do |f| %> ... <% end %> <%= form_for(@message, :remote => true) do |f| %> ... <% end %>
  • 21. Controller def create respond_to do |format| format.html { redirect_to(messages_url) } format.js { render :update do |page| ... end } end end
  • 23. $ rake db:test:clone_structure • $ rake test • $ ruby -Itest test/unit/user_test.rb • $ ruby -Itest test/unit/user_test.rb -n TEST_CASE_METHOD_NAME
  • 24. require 'test_helper' class UsersControllerTest < ActionController::TestCase test "should create a new user" do assert_difference("User.count", +1) do post :create, :user => { :email => "shaokun.wu@gmail.com", :password => "a" } assert_redirected_to root_path end end end
  • 25. require 'test_helper' class UserTest < ActiveSupport::TestCase test "should set the name through email" do user = User.new(:email => "shaokun.wu@gmail.com", :password => "a") assert user.save assert_equal "shaokun.wu", user.name end end
  • 26. Deploy App
  • 27. • PHP/ASP Upload • 200 / • Sina App Engine • Rails • VPS: linode.com, AWS EC2
  • 29. $ gem install heroku • $ heroku create roc-demo2 • $ heroku keys:add • $ git remote add heroku git@heroku.com:roc-demo2.git • $ git push heroku master • $ heroku rake db:migrate
  • 30. C:Sitesroc-demo2>git push heroku master Counting objects: 313, done. Compressing objects: 100% (185/185), done. Writing objects: 100% (313/313), 7.74 MiB | 15 KiB/s, done. Total 313 (delta 123), reused 232 (delta 94) -----> Heroku receiving push -----> Rails app detected -----> Detected Rails is not set to serve static_assets Installing rails3_serve_static_assets... done -----> Configure Rails 3 to disable x-sendfile Installing rails3_disable_x_sendfile... done -----> Configure Rails to log to stdout Installing rails_log_stdout... done -----> Gemfile detected, running Bundler version 1.0.7 Unresolved dependencies detected; Installing... Using --without development:test Fetching source index for http://rubygems.org/ Installing rake (0.8.7) ... Your bundle is complete! It was installed into ./.bundle/gems/ -----> Compiled slug size is 3.7MB -----> Launching... done, v4 http://roc-demo2.heroku.com deployed to Heroku To git@heroku.com:roc-demo2.git * [new branch] master -> master
  • 32. • Get the code: http://github.com/kudelabs/roc-demo2 • See it in action: http://demo2.railsoncampus.com • keep in touch: @railsoncampus http://weibo.com/1944841390