# Beginner/ Filtering logged users

**URL:** <https://www.ruby-forum.com/t/beginner-filtering-logged-users/165493>\
**Category:** Rails\
**Created:** [April 14, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/beginner-filtering-logged-users/165493 "2009-04-14T15:05:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fa00026](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fa00026](https://www.ruby-forum.com/u/fa00026)\
**Post date:** [April 14, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/beginner-filtering-logged-users/165493/1 "2009-04-14T15:05:07Z")

</div>

I just had an idea of banning the user from posting a new message  
unless logged in, how to use the filter in that case, below is where I  
want to put the filter (it is part of my posts controller) I have  
tried to put the filter inside it but it is not working. apologies for  
my question as I am a beginner in Ruby and Rails.

def new

```
@post = Post.new

respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @post }
end

```

end

Which is a part of the controller, called posts

class PostsController \< ApplicationController

# GET /posts

# GET /posts.xml

def index  
@posts = Post.find(:all)

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @posts }  
end  
end

# GET /posts/1

# GET /posts/1.xml

def show  
@post = Post.find(params[:id])  
@post\_comments = @post.comments.collect  
flash[:post\_id] =@post.id  
end

# GET /posts/new

# GET /posts/new.xml

def new

@post = Post.new

respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @post }  
end  
end

# GET /posts/1/edit

def edit  
@post = Post.find(params[:id])  
end

# POST /posts

# POST /posts.xml

def create

@post = Post.new(params[:post])

respond\_to do |format|  
if @post.save  
flash[:notice] = ‘Post was successfully created.’  
format.html { redirect\_to(@post) }  
format.xml { render :xml =\> @post, :status =\> :created, :location =\>  
@post }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @post.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# PUT /posts/1

# PUT /posts/1.xml

def update  
@post = Post.find(params[:id])

respond\_to do |format|  
if @post.update\_attributes(params[:post])  
flash[:notice] = ‘Post was successfully updated.’  
format.html { redirect\_to(@post) }  
format.xml { head Surprisedk }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @post.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# DELETE /posts/1

# DELETE /posts/1.xml

def destroy  
@post = Post.find(params[:id])  
@post.destroy

respond\_to do |format|  
format.html { redirect\_to(posts\_url) }  
format.xml { head Surprisedk }  
end  
end

# Adding a comment

def post\_comment  
@comment = Comment.new(  
“post\_id” =\> flash[:post\_id],  
“created\_at” =\> Time.now,  
“comment” =\> params[:comment][‘comment’]  
)  
flash[:notice] = ‘Comment was successfully added.’ if @comment.save  
redirect\_to :action =\> ‘show’, :id =\> flash[:post\_id]  
end  
end

I have been trying to modify this code which I got from the book agile  
Web D. with rails (the latest edition) as it has excatly the  
same concept because it also filters adminstrators users from any  
other users

Before\_filter :authorize, :except =\> :login

And this one also

protected  
def authorize  
unless User.find\_by\_id(session[:user\_id])  
session[:original\_uri] = request.request\_uri  
flash[:notice] = “Please log in”  
redirect\_to :controller =\> ‘admin’, :action =\> ‘login’  
end  
end  
end

Ah, of course I have developed the login in the admin controller  
Appreciate your help
