Skip to content

Instantly share code, notes, and snippets.

@SanjeQi
Created November 27, 2018 10:09
Show Gist options
  • Save SanjeQi/1d2e6422a403757af4f27c4eb1e2c6f3 to your computer and use it in GitHub Desktop.
Save SanjeQi/1d2e6422a403757af4f27c4eb1e2c6f3 to your computer and use it in GitHub Desktop.
class AuthorsController < ApplicationController
def create
@author = Author.new(author_params)
if @author.valid?
@author.save
redirect_to author_path(@author)
else
render :new
end
end
class Author < ActiveRecord::Base
validates :name, presence: true
validates :email, uniqueness: true
end
--------------------------------------------------------
class PostsController < ApplicationController
before_action :set_post!, only: [:show, :edit, :update]
def show
end
def edit
end
def update
@post.update(post_params)
if @post.valid?
@post.save
redirect_to post_path(@post)
else
render :edit
end
end
private
def post_params
params.permit(:category, :content, :title)
end
def set_post!
@post = Post.find(params[:id])
end
end
class Post < ActiveRecord::Base
validates :title, presence: true
validates :category, inclusion: { in: %w(Fiction Non-Fiction) }
validates :content, length: { minimum: 100 }
end
---------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment