Rails scaffold from scratch

541 views
446 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
541
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rails scaffold from scratch

  1. 1. www.hackyale.com< Intro to Rails />Monday, June 10, 13
  2. 2. Building MVCr from scratch< front end />Monday, June 10, 13
  3. 3. MVC RecapMonday, June 10, 13
  4. 4. Monday, June 10, 13
  5. 5. Monday, June 10, 13
  6. 6. hand-crafted,artisanalrails codeMonday, June 10, 13
  7. 7. routerMonday, June 10, 13
  8. 8. ROUTERthe router looks at the incoming requestand sends it to the right controller.Example:myapp.com/users/1/editgoes to the users controller, edit action(and sends a ‘parameter’ for user_id=1 as )Monday, June 10, 13
  9. 9. ROUTER/users users#index list all users/users/1 users#show show user with id 1/users/new users#new make a new user/users/1/edit users#edit edit user with id 1Monday, June 10, 13
  10. 10. SAMPLE ROUTERedditDemo::Application.routes.draw  do    match  users/  =>  users#index    root  to:  users#indexendMonday, June 10, 13
  11. 11. controllerMonday, June 10, 13
  12. 12. CONTROLLERThe controller orchestrates the request.It communicates with models to gatherand manipulate data.It then passes that data to the view.Monday, June 10, 13
  13. 13. CONTROLLER ACTIONSControllers have multiple actions.The standard ‘restful’ actions are:indexshowcreateneweditupdatedestroyMonday, June 10, 13
  14. 14. SAMPLE CONTROLLERclass  UsersController  <  ActionController::Base    def  index        @users  =  User.all    endendMonday, June 10, 13
  15. 15. viewsMonday, June 10, 13
  16. 16. VIEWSViews are templates for how your webpages look.Variables created in the respectivecontroller are used to insert relevantinformation into the template.Monday, June 10, 13
  17. 17. VIEWSViews use Embedded RuBy (ERB) tosubstitute data into the template.<% ruby.code.no.output %><%= equal.sign.outputs.to.template %>Monday, June 10, 13
  18. 18. SAMPLE VIEW<%  @users.each  do  |user|  %>    <%=  user.name  %><%  end  %>Monday, June 10, 13
  19. 19. modelsMonday, June 10, 13
  20. 20. MODELSModels represent the nouns and data inyour application.They make sure stored data is valid, andperform calculations/analysis.Monday, June 10, 13
  21. 21. DATABASES•Store our model data (in rows), and define theattributes (columns).•We manage changes in our database with migrations.Monday, June 10, 13
  22. 22. MIGRATION>> rails generate migration CreateUsersclass CreateUsers < ActiveRecord::Migrationdef changecreate_table :users do |t|t.string :first_namet.string :last_namet.string :netidt.timestampsendendendMonday, June 10, 13
  23. 23. DATABASEid first_name last_name netid123Adam Bray alb64Vladimir The Bear vtb39Charlie The Horse cth44usersMonday, June 10, 13
  24. 24. DATABASEid first_name last_name netid123Adam Bray alb64Vladimir The Bear vtb39Charlie The Horse cth44usersMonday, June 10, 13
  25. 25. ADDING ANOTHER COLUMNrails generate migrationAddClassYearToUsers class_year:integerMonday, June 10, 13
  26. 26. DATABASEid first_name last_name netid class_year123Adam Bray alb64 1995Vladimir The Bear vtb39 2015Charlie The Horse cth44 2016usersMonday, June 10, 13
  27. 27. SAMPLE MODELclass  User  <  ActiveRecord::BaseendMonday, June 10, 13
  28. 28. group activity:add show pagefor usersMonday, June 10, 13
  29. 29. YOU’LL NEED TO:•create a route•add a controller action•add a view templateMonday, June 10, 13
  30. 30. TIPSparams hash:if you put symbols in your route they are available in controllere.g. ‘users/:id’ gives you ‘params[:id]’ in controllerretrieving modelsuser.find(num) returns user with id == numuse link_to method to link from indexMonday, June 10, 13
  31. 31. group activity:add new userformMonday, June 10, 13
  32. 32. YOU’LL NEED TO•add two new routes• use ‘resources’ route instead•add two new actions, new, and create• new generates a form• create takes form data and uses it to create userMonday, June 10, 13
  33. 33. FORMS<%=  form_for(@user)  do  |f|  %>    <div  class="field">        <%=  f.label  :first_name  %><br  />        <%=  f.text_field  :first_name  %>    </div>    <div  class="actions">        <%=  f.submit  %>    </div><%  end  %>Monday, June 10, 13

×