Skip to content

Instantly share code, notes, and snippets.

@siebertm
Created September 4, 2009 09:11
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 siebertm/180808 to your computer and use it in GitHub Desktop.
Save siebertm/180808 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
belongs_to :user
end
module Users
class PostsController < ApplicationController
# a user can only see posts he created
def index
@posts = posts.paginate(:page => params[:page])
respond_with(@posts) #rails 3 goodness :)
end
def create
@post = current_user.posts.build(params[:post])
if @post.save
# ...
end
end
# same for destroy action
def update
@post = posts.find(params[:post_id])
# ...
end
protected
# returns a scope that is correct for the current user.
# this way, a user can only access his posts
def posts
current_user.posts
end
end
end
map.resources :users do |users|
users.resources :posts
end
class User < ActiveRecord::Base
has_many :posts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment