Skip to content

Instantly share code, notes, and snippets.

@jimmybaker
Created August 16, 2016 17:56
Show Gist options
  • Save jimmybaker/6013057876baec4f93812cbd97b43a27 to your computer and use it in GitHub Desktop.
Save jimmybaker/6013057876baec4f93812cbd97b43a27 to your computer and use it in GitHub Desktop.
Nested/Top level resource controller sharing
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :find_posts, only: [:index]
def index
end
def show
end
def edit
end
def update
end
def create
end
def destroy
end
private
def find_post
@post = Post.find(params[:id])
end
def find_posts
if params[:user_id]
@posts = user.posts
else
@posts = Post.all
end
end
def user
user = User.find(params[:user_id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment