狠狠撸

狠狠撸Share a Scribd company logo
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_con?rmation_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

More Related Content

What's hot (20)

PDF
Phinx talk
Michael Peacock
?
KEY
Phpne august-2012-symfony-components-friends
Michael Peacock
?
PDF
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
?
PDF
Building web framework with Rack
sickill
?
PDF
Getting Started-with-Laravel
Mindfire Solutions
?
PDF
Fabric Python Lib
Simone Federici
?
PPT
Web service with Laravel
Abuzer Firdousi
?
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
?
PDF
Rails2 Pr
xibbar
?
PPTX
An introduction to Laravel Passport
Michael Peacock
?
PPTX
Express JS
Alok Guha
?
PDF
Introduction to Rails - presented by Arman Ortega
arman o
?
PPTX
Wykorzystanie form request przy implementacji API w Laravelu
Laravel Poland MeetUp
?
PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
?
PPTX
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
?
PPT
Slim RedBeanPHP and Knockout
Vic Metcalfe
?
PDF
RESTful web services
Tudor Constantin
?
PDF
Perl web frameworks
diego_k
?
PDF
Rails3 changesets
Wen-Tien Chang
?
PPTX
Service approach for development REST API in Symfony2
Sumy PHP User Grpoup
?
Phinx talk
Michael Peacock
?
Phpne august-2012-symfony-components-friends
Michael Peacock
?
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
?
Building web framework with Rack
sickill
?
Getting Started-with-Laravel
Mindfire Solutions
?
Fabric Python Lib
Simone Federici
?
Web service with Laravel
Abuzer Firdousi
?
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
?
Rails2 Pr
xibbar
?
An introduction to Laravel Passport
Michael Peacock
?
Express JS
Alok Guha
?
Introduction to Rails - presented by Arman Ortega
arman o
?
Wykorzystanie form request przy implementacji API w Laravelu
Laravel Poland MeetUp
?
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
?
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
?
Slim RedBeanPHP and Knockout
Vic Metcalfe
?
RESTful web services
Tudor Constantin
?
Perl web frameworks
diego_k
?
Rails3 changesets
Wen-Tien Chang
?
Service approach for development REST API in Symfony2
Sumy PHP User Grpoup
?

Viewers also liked (8)

KEY
VIM for the PHP Developer
John Congdon
?
PPT
Rest Ruby On Rails
shaokun
?
PPT
WebSocket 实时推特流
shaokun
?
KEY
Rack
shaokun
?
KEY
iOS 图片浏览器 DIY
shaokun
?
PPT
Git flow
shaokun
?
PPTX
Rails Engine | Modular application
mirrec
?
KEY
Namespace less engine
shaokun
?
VIM for the PHP Developer
John Congdon
?
Rest Ruby On Rails
shaokun
?
WebSocket 实时推特流
shaokun
?
Rack
shaokun
?
iOS 图片浏览器 DIY
shaokun
?
Git flow
shaokun
?
Rails Engine | Modular application
mirrec
?
Namespace less engine
shaokun
?
Ad

Similar to More to RoC weibo (20)

ZIP
Rails 3 (beta) Roundup
Wayne Carter
?
PDF
Ruby on Rails - Introduction
Vagmi Mudumbai
?
PDF
RoR 101: Session 5
Rory Gianni
?
PDF
Rails 3: Dashing to the Finish
Yehuda Katz
?
PDF
Rails 4.0
Robert Gogolok
?
KEY
Rails Routing and URL design
hiq5
?
PDF
Rails vs Web2py
jonromero
?
PDF
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
?
PDF
RubyOnRails-Cheatsheet-BlaineKendall
tutorialsruby
?
PDF
RubyOnRails-Cheatsheet-BlaineKendall
tutorialsruby
?
PDF
SOLID Ruby SOLID Rails
Michael Mahlberg
?
PDF
Ruby on Rails : 簡介與入門
Wen-Tien Chang
?
PDF
How to disassemble one monster app into an ecosystem of 30
fiyuer
?
PDF
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
?
PPTX
Ruby on Rails Tutorial Part I
Wei Jen Lu
?
PDF
Rails 3 : Cool New Things
Y. Thong Kuah
?
KEY
Impression of Rails 3
Kosuke Matsuda
?
PPTX
12 Introduction to Rails
Deepak Hagadur Bheemaraju
?
PDF
Gigigo Rails Workshop
Alex Rupérez
?
PDF
Basic Rails Training
Arthit Hongchintakul
?
Rails 3 (beta) Roundup
Wayne Carter
?
Ruby on Rails - Introduction
Vagmi Mudumbai
?
RoR 101: Session 5
Rory Gianni
?
Rails 3: Dashing to the Finish
Yehuda Katz
?
Rails 4.0
Robert Gogolok
?
Rails Routing and URL design
hiq5
?
Rails vs Web2py
jonromero
?
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
?
RubyOnRails-Cheatsheet-BlaineKendall
tutorialsruby
?
RubyOnRails-Cheatsheet-BlaineKendall
tutorialsruby
?
SOLID Ruby SOLID Rails
Michael Mahlberg
?
Ruby on Rails : 簡介與入門
Wen-Tien Chang
?
How to disassemble one monster app into an ecosystem of 30
fiyuer
?
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
?
Ruby on Rails Tutorial Part I
Wei Jen Lu
?
Rails 3 : Cool New Things
Y. Thong Kuah
?
Impression of Rails 3
Kosuke Matsuda
?
12 Introduction to Rails
Deepak Hagadur Bheemaraju
?
Gigigo Rails Workshop
Alex Rupérez
?
Basic Rails Training
Arthit Hongchintakul
?
Ad

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_con?rmation_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