Getting started with Rails (2), Season 2

  • 1,605 views
Uploaded on

This is the screencast of offline lecture on Rails Guides at the 2nd round of ROR Lab. Season 2

This is the screencast of offline lecture on Rails Guides at the 2nd round of ROR Lab. Season 2

More in: Education , Technology
  • 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,605
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
8
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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. ROR Lab. Season 2 - The 2nd Round -Getting Started with Rails (2) July 7, 2012 Hyoseong Choi ROR Lab.
  • 2. A Blog Project Post ny on a mseparate form nested form e to form_for to ne m an o y Comment Tag form_for fields_for ROR Lab.
  • 3. Using Scaffolding$ rails generate scaffold Post name:string title:string content:textScaffolding Generator • MVC • asset • helper • test unit • routing ROR Lab.
  • 4. Creatinga Resource w scaffolding $ rails g scaffold post name title content:text invoke active_record create db/migrate/20120705045702_create_posts.rb create app/models/post.rb invoke test_unit create test/unit/post_test.rb create test/fixtures/posts.yml invoke resource_route route resources :posts invoke scaffold_controller create app/controllers/posts_controller.rb invoke erb create app/views/posts create app/views/posts/index.html.erb create app/views/posts/edit.html.erb create app/views/posts/show.html.erb create app/views/posts/new.html.erb create app/views/posts/_form.html.erb invoke test_unit create test/functional/posts_controller_test.rb invoke helper create app/helpers/posts_helper.rb invoke test_unit create test/unit/helpers/posts_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/posts.js.coffee invoke scss create app/assets/stylesheets/posts.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss ROR Lab.
  • 5. Running a Migrationclass CreatePosts < ActiveRecord::Migration  def change    create_table :posts do |t|      t.string :name      t.string :title      t.text :content       t.timestamps    end  endend db/migrate/20100207214725_create_posts.rb ROR Lab.
  • 6. Running a Migration$ rake db:migrate==  CreatePosts: migrating====================================================-- create_table(:posts)   -> 0.0019s==  CreatePosts: migrated (0.0020s) to Where? ROR Lab.
  • 7. Adding a Link<h1>Hello, Rails!</h1><%= link_to "My Blog", posts_path %> app/views/home/index.html.erb Hello, Rails! My Blog ROR Lab.
  • 8. The Modelclass Post < ActiveRecord::Base  attr_accessible :content, :name, :titleend app/models/post.rbRails ActiveModel Record database.yml ROR Lab.
  • 9. Adding Validations class Post < ActiveRecord::Base   attr_accessible :content, :name, :title     validates :name,  :presence => true   validates :title, :presence => true,                     :length => { :minimum => 5 } end app/models/post.rb app/models/post.rb ROR Lab.
  • 10. Using the Console $ rails console --sandbox $ rails c --sandbox Loading development environment in sandbox (Rails 3.2.6) Any modifications you make will be rolled back on exit 1.9.3p194 :001 > Post.all Post Load (0.1ms) SELECT "posts".* FROM "posts" => [] 1.9.3p194 :002 > reload! Reloading... => true ROR Lab.
  • 11. Listing All Postsdef index  @posts = Post.all   respond_to do |format|    format.html  # index.html.erb    format.json  { render :json => @posts }  endend app/controllers/posts_controller.rb1.9.3p194 :004 > posts = Post.all Post Load (0.2ms) SELECT "posts".* FROM "posts" => []1.9.3p194 :005 > posts.class => Array1.9.3p194 :006 > post = Post.scoped Post Load (0.2ms) SELECT "posts".* FROM "posts" => []1.9.3p194 :007 > post.class => ActiveRecord::Relation ROR Lab.
  • 12. <h1>Listing posts</h1> Rails makes all of the <table> instance variables from the  <tr> action available to the view.    <th>Name</th>    <th>Title</th>    <th>Content</th>    <th></th>    <th></th>    <th></th>  </tr> <% @posts.each do |post| %>  <tr>    <td><%= post.name %></td>    <td><%= post.title %></td>    <td><%= post.content %></td>    <td><%= link_to Show, post %></td>    <td><%= link_to Edit, edit_post_path(post) %></td>    <td><%= link_to Destroy, post, :confirm => Are you sure?,                                     :method => :delete %></td>  </tr><% end %></table> <br /> <%= link_to New post, new_post_path %> app/views/posts/index.html.erb ROR Lab.
  • 13. The Layout : Containers for views<!DOCTYPE html><html><head>  <title>Blog</title>  <%= stylesheet_link_tag "application" %>  <%= javascript_include_tag "application" %>  <%= csrf_meta_tags %></head><body style="background: #EEEEEE;"> <%= yield %> </body> app/views/layouts/application.html.erb ROR Lab.
  • 14. Creating New Posts def new   @post = Post.new     respond_to do |format|     format.html  # new.html.erb     format.json  { render :json => @post }   end end <h1>New post</h1>   <%= render form %> # using HTTP POST verb   <%= link_to Back, posts_path %> app/views/posts/new.html.erb ROR Lab.
  • 15. <%= form_for(@post) do |f| %>  <% if @post.errors.any? %>  <div id="errorExplanation">    <h2><%= pluralize(@post.errors.count, "error") %> prohibited        this post from being saved:</h2>    <ul>    <% @post.errors.full_messages.each do |msg| %>      <li><%= msg %></li>    <% end %>    </ul>  </div>  <% end %>   <div class="field">    <%= f.label :name %><br />    <%= f.text_field :name %>  </div>  <div class="field">    <%= f.label :title %><br />    <%= f.text_field :title %>  </div>  <div class="field">    <%= f.label :content %><br />    <%= f.text_area :content %>  </div>  <div class="actions">    <%= f.submit %>  </div><% end %> a “Partial” template app/views/posts/_form.html.erb ROR Lab. intelligent submit helper
  • 16. ROR Lab.
  • 17. “create” action ROR Lab.
  • 18. def create  @post = Post.new(params[:post])   respond_to do |format|    if @post.save      format.html  { redirect_to(@post,                    :notice => Post was successfully created.) }      format.json  { render :json => @post,                    :status => :created, :location => @post }    else      format.html  { render :action => "new" }      format.json  { render :json => @post.errors,                    :status => :unprocessable_entity }    end  endend ROR Lab.
  • 19. Show a Post http://localhost:3000/posts/1def show  @post = Post.find(params[:id])   respond_to do |format|    format.html  # show.html.erb    format.json  { render :json => @post }  endend ROR Lab.
  • 20. Show a Post app/views/posts/show.html.erb<p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p>  <b>Title:</b>  <%= @post.title %></p> <p>  <b>Content:</b>  <%= @post.content %></p>  <%= link_to Edit, edit_post_path(@post) %> |<%= link_to Back, posts_path %> ROR Lab.
  • 21. Editing Postsdef edit  @post = Post.find(params[:id])<h1>Editing post</h1> <%= render form %> # using HTTP PUT verb <%= link_to Show, @post %> |<%= link_to Back, posts_path %> app/views/posts/edit.html.erb ROR Lab.
  • 22. Editing Postsdef update  @post = Post.find(params[:id])   respond_to do |format|    if @post.update_attributes(params[:post])      format.html  { redirect_to(@post,                    :notice => Post was successfully updated.) }      format.json  { head :no_content }    else      format.html  { render :action => "edit" }      format.json  { render :json => @post.errors,                    :status => :unprocessable_entity }    end  endend ROR Lab.
  • 23. Destroying a Post def destroy   @post = Post.find(params[:id])   @post.destroy     respond_to do |format|     format.html { redirect_to posts_url }     format.json { head :no_content }   end ROR Lab.
  • 24. Git• Linus Tobalds, 1991• for source code version control• local repository ROR Lab.
  • 25. Gitosis• Remote repository• git clone git://eagain.net/gitosis.git• git clone git://github.com/res0nat0r/ gitosis.git• Ubuntu 11.10 -> gitosis package• Ubuntu 12.04 -> not any more but gitolite ROR Lab.
  • 26. Gitosis• Remote repository• git clone git://eagain.net/gitosis.git• git clone git://github.com/res0nat0r/ gitosis.git• Ubuntu 11.10 -> gitosis package• Ubuntu 12.04 -> not any more but gitolite ROR Lab.
  • 27. Local Machine Git Repository Sever git@gitserver:project.git gitosis gitolite gitorious$ git add *$ git commit$ git push Origin ROR Lab.
  • 28. 감사합니다.
  • 29.    ROR Lab.