• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Getting started with Rails (4), Season 2
 

Getting started with Rails (4), Season 2

on

  • 2,286 views

This is a Keynote slide about the "Getting started with Rails

This is a Keynote slide about the "Getting started with Rails

Statistics

Views

Total Views
2,286
Views on SlideShare
1,360
Embed Views
926

Actions

Likes
0
Downloads
9
Comments
0

12 Embeds 926

http://rorlab.org 684
http://lab.wordstory.net 137
http://www.rorlab.org 45
http://localhost 39
http://kt.rorlab.org 9
http://ror.la 3
http://livecoding.rorlab.org 3
https://www.facebook.com 2
http://www.hanrss.com 1
https://si0.twimg.com 1
http://webcache.googleusercontent.com 1
http://211.60.184.138 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Getting started with Rails (4), Season 2 Getting started with Rails (4), Season 2 Presentation Transcript

  • ROR Lab. Season 2 - The 4th Round -Getting Started with Rails (4) August 11, 2012 Hyoseong Choi ROR Lab.
  • 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.
  • Generating the 3rd Model : Tag $ rails generate model tag name:string post:references Migration file Model Class : db/migrate/xxxx_create_tag.rb : app/models/tag.rb Tag tags $ rake db:migrate belongs_to :post post_id :integer post:references ROR Lab.
  • Nested Attributes• save the child object through the parent object• Off, by default• switch “On”, by calling #accepts_nested_attributes_for • an attribute writer automatically defined on the parent model • :autosave option automatically enabled http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html ROR Lab.
  • Building a Multi- Model Form ✔class Post < ActiveRecord::Base  attr_accessible :content, :name, :title, :tags_attributes  validates :name,  :presence => true  validates :title, :presence => true,                    :length => { :minimum => 5 }   has_many :comments, :dependent => :destroy  has_many :tags  ✔  accepts_nested_attributes_for :tags, :allow_destroy => :true,    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }end ✔ multi-model form or nested form or form in form ROR Lab.
  • #accepts_nested _attributes_for• tags_attributes=(attributes)• :allow_destroy => true “_destroy” key, evaluated to true• :reject_if => proc { |attributes| attributes[‘name’].blank? }• :reject_if => proc { |attrs| attrs.all? { |k,v| v.blank?} } ROR Lab.
  • build vs new• new ➞ for Class ex) post = Post.new• build ➞ for association proxy ex) comment = post.comments.build ✓ automatic setting its foreign key for the new child object with patient object id ROR Lab.
  • <% @post.tags.build %><%= form_for(@post) do |post_form| %>   <div class="field">    <%= post_form.label :name %><br />    <%= post_form.text_field :name %>  </div>  <div class="field">    <%= post_form.label :title %><br />    <%= post_form.text_field :title %>  </div>  <div class="field">    <%= post_form.label :content %><br />    <%= post_form.text_area :content %>  </div>  <h2>Tags</h2>  <%= post_form.fields_for :tags do |tag_form| %>   <div class="field">     <%= tag_form.label :name, Tag: %>     <%= tag_form.text_field :name %>   </div>   <% unless tag_form.object.nil? || tag_form.object.new_record? %>     <div class="field">       <%= tag_form.label :_destroy, Remove: %>       <%= tag_form.check_box :_destroy %>     </div>   <% end %> <% end %>  <div class="actions">    <%= post_form.submit %>  </div><% end %> ✔ app/views/posts/_form.html.erb ROR Lab.
  • <% @post.tags.build %><%= form_for(@post) do |post_form| %>   <div class="field">    <%= post_form.label :name %><br />    <%= post_form.text_field :name %>  </div>  <div class="field">    <%= post_form.label :title %><br />    <%= post_form.text_field :title %>  </div>  <div class="field">    <%= post_form.label :content %><br /> accets_nested_attributes_for :tags    <%= post_form.text_area :content %>  </div>  <h2>Tags</h2>  <%= post_form.fields_for :tags do |tag_form| %>   <div class="field">     <%= tag_form.label :name, Tag: %>     <%= tag_form.text_field :name %>   </div>   <% unless tag_form.object.nil? || tag_form.object.new_record? %>     <div class="field">       <%= tag_form.label :_destroy, Remove: %>       <%= tag_form.check_box :_destroy %>     </div>   <% end %> <% end %>  <div class="actions">    <%= post_form.submit %>  </div><% end %> ✔ app/views/posts/_form.html.erb ROR Lab.
  • <% @post.tags.build %><%= form_for(@post) do |post_form| %>   <div class="field">    <%= post_form.label :name %><br />    <%= post_form.text_field :name %>  </div>  <div class="field">    <%= post_form.label :title %><br />    <%= post_form.text_field :title %>  </div>  <div class="field">    <%= post_form.label :content %><br /> accets_nested_attributes_for :tags    <%= post_form.text_area :content %>  </div> tags_attributes  <h2>Tags</h2>  <%= post_form.fields_for :tags do |tag_form| %>   <div class="field">     <%= tag_form.label :name, Tag: %>     <%= tag_form.text_field :name %>   </div>   <% unless tag_form.object.nil? || tag_form.object.new_record? %>     <div class="field">       <%= tag_form.label :_destroy, Remove: %>       <%= tag_form.check_box :_destroy %>     </div>   <% end %> <% end %>  <div class="actions">    <%= post_form.submit %>  </div><% end %> ✔ app/views/posts/_form.html.erb ROR Lab.
  • <% @post.tags.build %><%= form_for(@post) do |post_form| %>  <% 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> <%= form.fields_for :tags do |tag_form| %>  </div>   <div class="field">  <% end %>     <%= tag_form.label :name, Tag: %>      <%= tag_form.text_field :name %>  <div class="field">   </div>    <%= post_form.label :name %><br />   <% unless tag_form.object.nil? || tag_form.object.new_record? %>    <%= post_form.text_field :name %>     <div class="field">  </div>       <%= tag_form.label :_destroy, Remove: %>  <div class="field">       <%= tag_form.check_box :_destroy %>    <%= post_form.label :title %><br />     </div>    <%= post_form.text_field :title %>   <% end %>  </div> <% end %>  <div class="field"> ✔ app/views/tags/_form.html.erb    <%= post_form.label :content %><br />    <%= post_form.text_area :content %>  </div>  <h2>Tags</h2>  <%= render :partial => tags/form,             :locals => {:form => post_form} %>  <div class="actions">    <%= post_form.submit %>  </div><% end %> ✔ app/views/posts/_form.html.erb ROR Lab.
  • <p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p> module PostsHelper  <b>Title:</b>   def join_tags(post)  <%= @post.title %>     post.tags.map { |t| t.name }.join(", ")</p>   end  end<p>  <b>Content:</b> ✔ app/helpers/posts_helper.rb  <%= @post.content %></p> <p> <p>  <b>Tags:</b>   <b>Tags:</b>  <%= @post.tags.map { |t| t.name }.join(", ") %>   <%= join_tags(@post) %></p> </p> <h2>Comments</h2><%= render @post.comments %> <h2>Add a comment:</h2><%= render "comments/form" %>  <%= link_to Edit Post, edit_post_path(@post) %> |<%= link_to Back to Posts, posts_path %> | ✔ app/views/posts/show.html.erb ROR Lab.
  • <p id="notice"><%= notice %></p> <p>  <b>Name:</b>  <%= @post.name %></p> <p> module PostsHelper  <b>Title:</b>   def join_tags(post)  <%= @post.title %>     post.tags.map { |t| t.name }.join(", ")</p>   end  end<p>  <b>Content:</b> ✔ app/helpers/posts_helper.rb  <%= @post.content %></p> <p> <p>  <b>Tags:</b>   <b>Tags:</b>  <%= join_tags(@post) %>   <%= join_tags(@post) %></p> </p> <h2>Comments</h2><%= render @post.comments %> <h2>Add a comment:</h2><%= render "comments/form" %>  <%= link_to Edit Post, edit_post_path(@post) %> |<%= link_to Back to Posts, posts_path %> | ✔ app/views/posts/show.html.erb ROR Lab.
  • Live DemoCreating a project ~ 3rd model, Tag ROR Lab.
  • 감사합니다.
  •    ROR Lab.