Ruby On Rails


Published on

Ruby on rails, components, ActiveRecord, Routing

Published in: Software, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ruby On Rails

  1. 1. Ruby on Rails Mehmet Beydogan 2014
  2. 2. Ruby on Rails Mehmet Beydogan 2014
  3. 3. Why Ruby? “Ruby is natural more than simple”
  4. 4. Everything is an object 5.times do puts “Hello World” end Hello World Hello World Hello World Hello World Hello World 5.class => Fixnum=> Fixnum nil.class => NilClass
  5. 5. Ruby Blocks patients.each do |patient| if patient.ill? physician.examine patient else patient.jump_for_joy! end end You can group a set of instructions together
  6. 6. Open Classes class String def write_size self.size end end size_writer = "Tell me my size!" puts size_writer.write_size In Ruby, classes are never closed, you can always add methods to an existing class.
  7. 7. Method Missing class Dummy def method_missing(m, *args, &block) puts "There's no method #{m}" end end method_missing is in part a safety net: It gives you a way to intercept unanswerable messages and handle them gracefully
  8. 8. Method Missing User.find_by_name User.find_by_email You can use regular expressions to more dynamic classes
  9. 9. Ruby on Rails
  10. 10. Rails -Convention Over Confuguration -DRY – Dont Repeat Yourself -MVC Princibles
  11. 11. Convention Over Confuguration Model: User Table: users Controller: users_controller
  12. 12. MVC
  13. 13. REST -simplifies component implementation -reduces the complexity of connector semantics,
  14. 14. Rails Components -Active Record -Database Migrations -Active Record Validations -Active Record Callbacks -Active Record Assocations Model Layer -Action View -Layouts & Partials -Rendering View Layer -Controller Overview -Routing Controller Layer
  15. 15. Rails Components -i18n -Action Mailer -Asset Pipeline -Generators More Components
  16. 16. Model Layer
  17. 17. Active Record -Represent models and their data -Represent associations between these models -Represent inheritance hierarchies through related models -Validate models before they get persisted to the database -Perform database operations in an object-oriented fashion.
  18. 18. Creating Active Record Models
  19. 19. Read
  20. 20. Update
  21. 21. Delete
  22. 22. Active Record Migrations app/db/migrations/create_publications.rb
  23. 23. Active Record Migrations Supported Methods add_column add_index add_reference add_timestamps create_table create_join_table drop_table drop_join_table remove_timestamps rename_column rename_index remove_reference rename_table
  24. 24. Running Migrations
  25. 25. Active Record Validations Validations are used to ensure that only valid data is saved into your database.
  26. 26. format
  27. 27. length
  28. 28. numericality
  29. 29. presence
  30. 30. uniqueness
  31. 31. Validation Errors
  32. 32. Active Record Callbacks -Active Record provides hooks into this object life cycle so that you can control your application and its data. -Callbacks allow you to trigger logic before or after an alteration of an object's state.
  33. 33. Callback Registration
  34. 34. When does it get triggered? -Creating an Object before_validation after_validation before_save around_save before_create around_create after_create after_save -Updating an Object before_validation after_validation before_save around_save before_update around_update after_update after_save -Destroying an Object before_destroy around_destroy after_destroy
  35. 35. Active Record Associations They make common operations simpler and easier in your code. -Get a user's all posts -Get the author of a post
  36. 36. Why Associations? Consider we have Order and Customer models
  37. 37. Why Associations? Creating an order
  38. 38. Why Associations? Deleting all orders of a customer while deleting a customer
  39. 39. IT'S PAINFUL!
  40. 40. Use Associations Creating an order Deleting a customer with it's orders
  41. 41. belongs_to
  42. 42. has_one
  43. 43. has_many
  44. 44. has_and_belongs_to_many
  45. 45. has_many through
  46. 46. Polymorphic
  47. 47. View Layer -Action View -Layouts & Partials -Rendering
  48. 48. Action View -Action View is responsible for compiling the response. -Action View templates are written using embedded Ruby in tags mingled with HTML. -All view files must be in app/views folder
  49. 49. app/views/posts/index.html.erb app/views/posts/new.html.erb app/views/posts/edit.html.erb app/views/posts/show.html.erb
  50. 50. Templates ERB: Embedded RuBy list_people.html.erb
  51. 51. Partials To simplify templates
  52. 52. Controller Layer -Controller Overview -Routing
  53. 53. Controller Layer -Controller is responsible for making sense of the request and producing the appropriate output -Rails favors pluralization of the last word in the controller's name ClientsController SiteAdminsController PostsController
  54. 54. Routing -The Rails router recognizes URLs and dispatches them to a controller's action. -Generate paths and URLs, avoiding the need to hardcode strings in your views. routes.rb Request index.html.erb
  55. 55. Where to start? - -
  56. 56. Questions?