#001 Rails Tutorial

| 31 minutes | 入门 博客
通过一个极简单的博客例子,展示一些基本的Ruby on Rails特性和开发方法。

涉及的一些代码:

Post Model:

ruby
class Post < ActiveRecord::Base
  validates :title, :presence => true, :uniqueness => true
  validates :content, :presence => true

  has_many :comments
end

Comment Model:

ruby
class Comment < ActiveRecord::Base
  belongs_to :post
end

Comments Controller:

ruby
class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])
    redirect_to @post if @comment.save
  end
end
Similar Episodes