Upload
buitu
View
214
Download
0
Embed Size (px)
Citation preview
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
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_ofvalidates_presence_ofvalidates_length_ofvalidates_acceptance_ofvalidates_associatedvalidates_confirmation_ofvalidates_exclusion_ofvalidates_format_ofvalidates_inclusion_ofvalidates_numericality_ofvalidates_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", "istoo short (minimum is 5 characters)"]}>
Create
p = Post.newp.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.firstPost.lastPost.all == Post.find(:all)Post.countPost.order(:title)Post.limit(10)Post.where(:name => "Béla")
Updatep = Post.find(2)p.title = "Új cím"p.save
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")
Kapcsolatok
$ script/generate model Comment commenter:string body:text post:references
class Comment < ActiveRecord::Basebelongs_to :postend
class Post < ActiveRecord::Base#validate...has_many :commentsend
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}
endend
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_pathnew_post_pathedit_post_path(post)post, :method => :delete
Átirányítás
Test::Application.routes.draw do |map|resources :postsmatch '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/