Forum: Ruby on Rails Beginner/ Filtering logged users

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
fa00026 (Guest)
on 2009-04-14 17:05
(Received via mailing list)
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
This topic is locked and can not be replied to.