• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,957
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
28
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ruby on Rails Wprowadzenie Klaudiusz Fabryczny, klodus@gmail.com Katowice, Spodek 2.0, 2007.10.13
  • 2. Ruby http://www.ruby-lang.org - twórca: Yukihiro Matsumoto, “Matz” - cechy języka: - darmowy, - o otwartym kodzie, - przenośny, - VHLL, - interpretowany, - w pełni objektowy, - dynamiczny, - łatwy do nauki, - zwięzły, - czytelny, - rozszerzalny, - aktywna społeczność, - reflection, - metaprograming.
  • 3. Ruby on Rails http://www.rubyonrails.com - twórca: David Heinemeier Hansson - cechy framework'u : - darmowy, - o otwartym kodzie, - web'owy, - wysoce produktywny, - wspiera Ajax'a, - posiada wbudowany webserver WEBrick, - system budowania rake, - mechanizm migracji, - ORM (Object Relation Mapping), - interaktywna konsola, - architektura MVC (Model – Viev – Controller), - “filozofia”: - DRY (Don't Repeat Yourself), - CoC (Convention over Configuration)
  • 4. Komponenty Ruby on Rails: - ActiveRecord, - ActionPack: - ActionController, - ActionView, - ActionWebService, - ActiveSupport, - ActionMailer, --------------------------- - ActiveResource - REST
  • 5. Struktura aplikacji (niepełna): Spodek app controllers helpers models views config environments database.yml environment.rb routes.rb db migrate 001_nazwa_migracji.rb doc lib taks log public images javascripts stylesheets script test tmp vendor plugins
  • 6. ActionPack http://aplikacja/admin/edit_user/1024 MVC /home/k/Spodek/app/controllers/admin_controller.rb class AdminController < ApplicationController def edit_user # kod metody end end MVC /home/k/Spodek/app/views/admin/edit_user.rhtml < % form_tag do |form| %> <%= render :partial =>”user” %> <%= submit_tag “Edytuj” %> <% end %>
  • 7. ActiveRecord Klasa odpowiada tabeli bazodanowej Instancja danej klasy konketnemu rekordowi w tabeli. Atrybut obiektu, polu w tabeli. MVC Tabela users /home/k/Spodek/app/models/user.rb class User < ActiveRecord::Base end
  • 8. Relacje “jeden do jednego” class Car < ActiveRecord::Base Class Owner < ActiveRecord::Base belongs_to :owner has_one :car end end
  • 9. “jeden do wielu” class Child < ActiveRecord::Base class Mother < ActiveRecord::Base belongs_to :mother has_many :children end end
  • 10. “wiele do wielu” class Author < ActiveRecord::Base Class Book < ActiveRecord::Base c l has_and_belongs_to_many :books has_and_belongs_to_many :authors a end end
  • 11. @book = Book.find(1024) @authors = @book.authors @author = Author.find(2048) @books_of_author = @author.books
  • 12. “Findery” @user = User.find_by_login_and_password(login, password) @user = User.find(:first, :conditions =>[“login = ? and password = ?”, login, password ] ) @order = Order.find_by_sql(“select id, name where id = 21”)
  • 13. Walidacja class User < ActiveRecord::Base validates_uniqueness_of :login, :message =>”Login musi być unikalny !!!” validates_confirmation_of :password validates_format_of :length, :with => /^d+(m|cm)/ validates_length_of :name, :maximum =>30 end
  • 14. “Callback'i” class User < ActiveRecord::Base before_destroy :check_rights def check_rights # kod metody end end
  • 15. Paginator def list_products @product_pages, @products = paginate (:products, :order_by =>'name') end <%= pagination_links(@product_pages) %>
  • 16. Layout'y class AdminController < ApplicationController layout “admin” end class UserController < ApplicationController layout “user”, :except => [:rss] end
  • 17. Migracja class CreateUsers < ActiveRecord::Migration def self.up create_table “users” do |f| f.column “f_name”, :string f.column “l_name”, :string end end def self.down drop_table “users” end end
  • 18. Rake rake db:migrate rake db:migrate VERSION = 12 rake tmp:clear rake log:clear rake -T
  • 19. AJAX <head> <%= javascript_include_tag “prototype” %> </head> <%= link_to_remote (“Show”, :update =>”mydiv”, :url => { :action =>:update_div} ) %> <div id=”mydiv”>Zmień mnie</div>
  • 20. Konsola ruby script/concole >>> u = Dog.find_by_name(“Mantas”) >>> u.id => 1 >>> u.rasa = “Spaniel” >>> u.save >>> d = Dog.find(12) >>> d.destroy
  • 21. Linki: http://www.ruby-lang.org http://www.rubyonrails.com http://www.37signals.com http://www.basecamphq.com http://www.railsenvy.com http://www.rubyinside.com http://railscasts.com http://www.pragprog.com http://wiki.rubyonrails.org/rails/pages/Plugins
  • 22. Książki: - Agile Web Development with Rails: Second Edition, 2nd Edition, - Practices of an Agile Developer, - Programming Ruby: The Pragmatic Programmer's Guide, 2nd Edition, - Rails Recipes, - From Java To Ruby: Things Every Manager Should Know, - Learn to Program (Ruby), - Enterprise Integration with Ruby, - Rails for PHP Developers, - Rails for Java Developers, - Beginning Ruby: From Novice to Professional, - Practical Ruby Gems, - Beginning Google Maps Applications with Rails and Ajax: From Novice to Professional.
  • 23. Dziękuję za uwagę :pytania => “klodus@gmail.com”