Created
November 27, 2018 10:09
-
-
Save SanjeQi/1d2e6422a403757af4f27c4eb1e2c6f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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