class Article ではバリデーションを宣言します。カラム、ファインダー、関連付けは db/schema.rb から生成されます。
config/routes.rbruby
Cybertrain::Routes.draw do root "articles#index"# A nested block takes the mapper explicitly (Spinel's instance_eval# trampoline only rewires `self` for the outermost block): `articles.resources`. resources :articlesdo |articles| articles.resources :comments, only: [:create, :destroy]endend
app/controllers/articles_controller.rbruby
# app/controllers/articles_controller.rb (index/show/edit are as plain as# Rails' own scaffold; update mirrors create)classArticlesController < ApplicationController before_action :set_article, only: [:show, :edit, :update, :destroy]def new_action # a `new` method would shadow ArticlesController.new@article = Article.newenddef create@article = Article.new(article_params)if@article.save flash[:notice] = "Article was successfully created." redirect_to article_path(@article), status::see_otherelse render :new, status::unprocessable_entityendendprivatedef set_article@article = Article.find(params[:id])enddef article_params params.require(:article).permit(:title, :body)endend
# app/models/article.rb -- only the first string field gets a default# presence validation; add anything past that yourself.classArticle validates :title, presence:true validates :body, presence:true, length: { minimum: 10 }end