Skip to content

Instantly share code, notes, and snippets.

@rockyjia
Last active January 2, 2017 11:01
Show Gist options
  • Select an option

  • Save rockyjia/0dea90505ada299df3f8274488b392cf to your computer and use it in GitHub Desktop.

Select an option

Save rockyjia/0dea90505ada299df3f8274488b392cf to your computer and use it in GitHub Desktop.
posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!, :only => [:new, :create, :update, :destroy]
def new
@group = Group.find(params[:group_id])
@post = Post.new
end
def show
end
def edit
@group = Group.find(params[:group_id])
@post = Post.find(params[:id])
end
def create
@group = Group.find(params[:group_id])
@post = Post.new(post_params)
@post.group = @group
@post.user = current_user
if @post.save
redirect_to group_path(@group)
else
render :new
end
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to account_posts_path, notice: "update success"
else
render :edit
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to account_posts_path, alert: "Post deleted."
end
private
def post_params
params.require(:post).permit(:content)
end
end
@rockyjia
Copy link
Author

rockyjia commented Jan 2, 2017

app/controllers/posts_controller.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment