Skip to content

Instantly share code, notes, and snippets.

@czepluch
Last active January 1, 2016 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czepluch/8090251 to your computer and use it in GitHub Desktop.
Save czepluch/8090251 to your computer and use it in GitHub Desktop.
class PostsController < ApplicationController
before_filter :authenticate_user!
def new
@post = Post.new
end
def create
@user = User.find(params[:user_id])
@post = @user.posts.create(params[:post].permit(:title, :description, :location))
redirect_to user_path(@user)
end
def destroy
@user = User.find(params[:user_id])
@post = @user.post.find(params[:id])
@post.destroy
redirect_to user_path(@user)
end
# Might not be needed anymore. Leaving it for reference
#def create
#@post = Post.new(post_params)
#if @post.save
#redirect_to @post
#else
#render 'new'
#end
#end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :description, :location))
redirect_to @post
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :description, :location)
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def index
@users = User.all
end
# this is just a long shot at a solution
def all
@posts = Post.all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment