| View previous topic :: View next topic |
| Author |
Message |
fa00026
Joined: 20 Feb 2009 Posts: 29
|
Posted: Mon Apr 13, 2009 10:01 pm Post subject: How to filter logged user? |
|
|
Hello,
I wish someone can help me with this:
I building a forum, which users are able to post replies and new posts only if they are logged in.
So i have this 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 k }
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 k }
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 Development 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
Please can I have any enlightment about how to connect the code in the book into my controller?
Appreciate it a lot |
|
| Back to top |
|
 |
Anthony Richardson Posted via mailing list.
|
Posted: Mon Apr 13, 2009 10:24 pm Post subject: How to filter logged user? |
|
|
Hi,
This list/forum is for support on how to use NetBeans with Ruby and
not a general support forum for how to use Ruby On Rails. You
questions are off topic and *may* get a response, but it is unlikely.
I suggest you use the support options listed here (
http://rubyonrails.org/community ) to get a timely response to your
questions.
Cheers,
Anthony
On Tue, Apr 14, 2009 at 7:31 AM, fa00026 <address-removed> wrote:
| Quote: |
Hello,
I wish someone can help me with this:
I building a forum, which users are able to post replies and new posts only if they are logged in.
So i have this controller, called posts
class PostsController < ApplicationController
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|
|
|