Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PostsController < ApplicationController
- before_action :authenticate_user!
- before_action :set_post, only: [:show, :edit, :update, :destroy]
- # GET /posts
- # GET /posts.json
- def index
- @posts = Post.page(params[:page]).per(5)
- end
- # GET /posts/1
- # GET /posts/1.json
- def show
- end
- # GET /posts/new
- def new
- @post = Post.new
- end
- # GET /posts/1/edit
- def edit
- end
- # POST /posts
- # POST /posts.json
- def create
- @post = Post.new(post_params)
- @post.author_id = current_user.id
- respond_to do |format|
- if @post.save
- format.html { redirect_to @post, notice: 'Post was successfully created.' }
- format.json { render action: 'show', status: :created, location: @post }
- else
- format.html { render action: 'new' }
- format.json { render json: @post.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /posts/1
- # PATCH/PUT /posts/1.json
- def update
- respond_to do |format|
- if @post.update(post_params)
- format.html { redirect_to @post, notice: 'Post was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @post.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /posts/1
- # DELETE /posts/1.json
- def destroy
- @post.destroy
- respond_to do |format|
- format.html { redirect_to posts_url }
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_post
- @post = Post.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def post_params
- params.require(:post).permit(:author_id, :body, :title)
- end
- end
- class Post < ActiveRecord::Base
- validates :author_id, presence: true
- belongs_to :author,
- class_name: "User",
- foreign_key: :author_id,
- primary_key: :id
- has_many :comments, class_name: "Comment"
- has_many :commenters, through: :comments, source: :user
- default_scope { order("created_at DESC") }
- end
- class CommentsController < ApplicationController
- before_action :authenticate_user!
- before_filter :load_post
- before_action :set_comment, only: [:show, :edit, :update, :destroy]
- # GET /comments
- # GET /comments.json
- def index
- @comments = @post.comments.all
- end
- # GET /comments/1
- # GET /comments/1.json
- def show
- @comment = @post.comments.find(params[:id])
- end
- # GET /comments/new
- def new
- @comment = @post.comments.new
- end
- # GET /comments/1/edit
- def edit
- @comment = @post.comments.find(params[:id])
- end
- # POST /comments
- # POST /comments.json
- def create
- @comment = @post.comments.new(comment_params)
- @comment.author_id = current_user.id
- @comment.post_id = @post.id
- respond_to do |format|
- if @comment.save
- format.html { redirect_to @post, notice: 'Comment was successfully created.' }
- format.json { render action: 'show', status: :created, location: @post }
- else
- format.html { render action: 'new' }
- format.json { render json: @comment.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /comments/1
- # PATCH/PUT /comments/1.json
- def update
- @comment = @post.comments.find(params[:id])
- respond_to do |format|
- if @comment.update(comment_params)
- format.html { redirect_to @post, notice: 'Comment was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @comment.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /comments/1
- # DELETE /comments/1.json
- def destroy
- @comment = @post.comments.find(params[:id])
- @comment.destroy
- respond_to do |format|
- format.html { redirect_to @post }
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def load_post
- @post = Post.find(params[:post_id])
- end
- def set_comment
- @comment = Comment.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def comment_params
- params.require(:comment).permit(:author_id, :post_id, :body, :title)
- end
- end
- class Comment < ActiveRecord::Base
- validates :post, :body, presence: true
- belongs_to :post,
- class_name: "Post",
- foreign_key: :post_id,
- primary_key: :id
- belongs_to :author,
- class_name: "User",
- foreign_key: :author_id,
- primary_key: :id
- default_scope { order("created_at DESC") }
- end
- class User < ActiveRecord::Base
- # Include default devise modules. Others available are:
- # :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- has_many :posts, class_name: "Post", foreign_key: :author_id, primary_key: :id
- has_many :comments
- has_many :commented_posts, through: :comments, source: :post
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement