Created
August 16, 2016 17:56
-
-
Save jimmybaker/6013057876baec4f93812cbd97b43a27 to your computer and use it in GitHub Desktop.
Nested/Top level resource controller sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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