Jak ustawić dla całego kontrolera akcję dla current_user, poza tym ten kod wydaje mi się zbytnio skomplikowany jest on napisany na podstawie tutoriala do rails 2.
[code=ruby]class TopicsController < ApplicationController
def show
@topic = Topic.find(params[:id])
end
def new
@post = Post.new
@topic = Topic.new
end
def create
if current_user == nil
params[:topic][:last_poster_id] = 1
params[:topic][:user_id] = 1
else
params[:topic][:last_poster_id] = current_user.id
params[:topic][:user_id] = current_user.id
end
params[:topic][:last_post_at] = Time.now
@topic = Topic.new(params[:topic])
if @topic.save
@topic = Topic.new(:name => params[:topic][:name],
:last_poster_id => params[:topic][:last_poster_id],
:last_post_at => params[:topic][:last_post_at],
:forum_id => params[:topic][:forum_id],
:user_id => params[:topic][:user_id])
if current_user == nil
@post = Post.new(:content => params[:post][:content],
:topic_id => Topic.last.id,
:user_id => 1)
else
@post = Post.new(:content => params[:post][:content],
:topic_id => Topic.last.id,
:user_id => current_user.id)
end
if @post.save
flash[:notice] = "Successfully created topic."
redirect_to "/forums/#{@topic.forum_id}"
else
redirect :action => 'new'
end
else
render :action => ‘new’
end
end[/code]