Created
September 4, 2009 09:11
-
-
Save siebertm/180808 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Post < ActiveRecord::Base | |
belongs_to :user | |
end |
This file contains hidden or 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
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 |
This file contains hidden or 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
map.resources :users do |users| | |
users.resources :posts | |
end |
This file contains hidden or 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 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