Ruby on Rails
Mi is ez? A Ruby nyelvre épülő nyílt forráskódú keresztplatformos MVC (Model-View-Controller) mintára épülő webalkalmazás-keretrendszer
Történet David Heinemeier Hansson készítette Első kiadás: 2004. július 1.0-s verzió: 2005. december Legújabb verzió: 3.0.7
Alapelvek DRY: Don't Repeat Yourself CoC: Convention over Configuration REST: Representational State Transfer
Komponensek Action Pack Action Controller Action Dispatch Action View Action Mailer Active Model Active Record Active Resource Active Support Railties
Telepítés Szükséges szoftverek: ruby, irb rails webszerver, pl. lighttpd adatbázis, pl. mysql Ha mindez megvan: $ rails test $ cd test $ ruby script/server
Scaffold $ script/generate scaffold Post name:string title:string content:text $ rake db:migrate Ez létrehozza a posts nevű táblát, ami tartalmazza a fenti oszlopokat, illetve a Post nevű osztályt, ami ennek elérésére szolgál.
Modell A Post osztály kódja pedig így fog kinézni: class Post < ActiveRecord::Base end
Modell Ezt kibővíthetjük pl. ellenőrzésekkel: class Post < ActiveRecord::Base validates_presence_of :name,:title validates_length_of :title,:minimum => 5 end
Validációk típusai validates_uniqueness_of validates_presence_of validates_length_of validates_acceptance_of validates_associated validates_confirmation_of validates_exclusion_of validates_format_of validates_inclusion_of validates_numericality_of validates_with
Rails konzol $ script/console >> p = Post.create(:content => "Egy új post") => #<Post id: nil, name: nil, title: nil, content: "Egy új post", created_at: nil, updated_at: nil> >> p.save => false >> p.errors => #<ActiveRecord::Errors:0x23bcf0c @base=#<post id: nil, name: nil, title: nil, content: "Egy új post", created_at: nil, updated_at: nil>, @errors={"name"=>["can't be blank"], "title"=>["can't be blank", "is too short (minimum is 5 characters)"]}>
CRUD Create Read Update Delete
Create p = Post.new p.name = "Béla" p.title = "Valami" p.content = "Bla" p.save p = Post.new(:name => "Béla", :title => "Valami", :content => "Bla") p.save p.create(:name => "Béla", :title => "Valami", :content => "Bla")
Read Post.find(2) Post.find(2, 3, 4) Post.first Post.last Post.all == Post.find(:all) Post.count Post.order(:title) Post.limit(10) Post.where(:name => "Béla")
p = Post.find(2) p.title = "Új cím" p.save Update p = Post.find(2) p.attributes = {:name => "Béla", :title => "Valami", :content => "Bla"} p.save p = Post.find(2) p.update_attributes(:name => "Béla", :title => "Valami", :content => "Bla")
Delete p = Post.find(2) p.destroy Post.find(2).destroy Post.destroy_all
Kapcsolatok $ script/generate model Comment commenter:string body:text post:references class Comment < ActiveRecord::Base belongs_to :post end class Post < ActiveRecord::Base #validate... has_many :comments end
Kapcsolatok fajtái belongs_to has_one has_many has_and_belongs_to_many
Egy oldal létrehozása app/controllers/posts_controller.rb def index @posts = Post.find(:all) respond_to do format format.html # index.html.erb format.xml {render :xml => @posts} format.json {render :json => @posts} end end
Ruby kód HTML-be ágyazása app/view/posts/index.html.erb <!--...--> <% for post in @posts %> <tr> <td><%= post.name %></td> <td><%= post.title %></td> <td><%= post.content %></td> <!--...-->
Layoutok app/views/layouts/posts.html.erb <html> <head> <!-- --> </head> <body> <p style="color: green"> <%= flash[:notice] %> </p> <%= yield %> </body> </html>
Linkek <%= link_to comment.post.title, post_path(comment.post) %> # vagy <%= link_to comment.post.title, comment.post %> posts_path new_post_path edit_post_path(post) post, :method => :delete
Átirányítás Test::Application.routes.draw do map resources :posts match 'new_post' => 'Posts#new' match 'all' => redirect('/posts') match 'by_month/:month' => 'Posts#index' match ':name' => 'Posts#index', :as => 'user_posts' end
További információ http://rubyonrails.org http://api.rubyonrails.org http://apidock.com/rails http://railsapi.com/ http://guides.rubyonrails.org/getting_started.html http://railsforzombies.org/ http://railscasts.com/