creddit/app/controllers/posts_controller.rb

76 lines
1.3 KiB
Ruby
Raw Normal View History

2015-12-11 15:26:51 -05:00
# controllers/posts_controller.rb
2015-07-14 16:29:22 -04:00
class PostsController < ApplicationController
before_filter :set_post, except: [:index, :new, :create]
before_filter :set_subcreddit
2015-12-14 13:28:39 -05:00
after_action :verify_authorized
2015-07-14 16:29:22 -04:00
2015-08-12 16:45:30 -04:00
def index
2015-12-11 15:11:06 -05:00
@posts = Post.includes(:subcreddit, :user).all
2015-12-14 13:28:39 -05:00
authorize Post
2015-08-12 16:45:30 -04:00
end
2015-07-14 16:29:22 -04:00
def show
2015-12-11 15:11:06 -05:00
@comments = @post.comments.includes(:user).arrange(order: :created_at)
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
end
def new
@post = Post.new
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
end
def create
@post = @subcreddit.posts.build(post_params)
@post.user = current_user
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
if @post.save
redirect_to subcreddit_post_path(@subcreddit, @post),
notice: 'Post created'
else
render :new
end
end
def edit
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
end
def update
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
if @post.update(post_params)
redirect_to subcreddit_post_path(@subcreddit, @post),
notice: 'Post was updated'
else
render :edit
end
end
def destroy
2015-12-14 13:28:39 -05:00
authorize @post
2015-07-14 16:29:22 -04:00
@post.destroy
redirect_to subcreddits_path(@subcreddit), notice: 'Post was deleted'
end
private
def post_params
params.require(:post).permit(:title, :link, :content, :subcreddit_id)
end
def set_subcreddit
@subcreddit = Subcreddit.find_by_slug(params['subcreddit_id'])
end
def set_post
@post = Post.find(params[:id])
end
end