# Logging actions

**URL:** <https://www.ruby-forum.com/t/logging-actions/77252>\
**Category:** Rails\
**Created:** [November 25, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/logging-actions/77252 "2006-11-25T16:05:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 25, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/logging-actions/77252/1 "2006-11-25T16:05:05Z")

</div>

Hey,

I want to log the actions of the users.  
I do it like this

when creating a product:

in the controller

def create  
@product = Product.new(params[:product])  
if @product.save  
if isLogging()  
createLog(@product,controller) #here i need to pass the controller or  
just the action

```
	end
	flash[:notice] = 'Product was successfully created.'
	redirect_to :action => 'show', :id => @product
else
	render :action => 'new'
end

```

end

in the application controller  
def isLogging()  
return true  
end

def createLog(obj,the\_controller)  
log = LogFile.new()  
log.the\_class = obj.class  
log.the\_class\_id = obj.id  
log.the\_action = the\_controller.action\_name  
if User.backend\_user != nil  
log.user\_name = User.backend\_user.user\_name  
end  
log.save  
end

It doesnt know “controller”,  
i want to use it like this

createLog(@product,controller)

or

createLog(@product,controller.action\_name)

can sombody help me?

Thx.  
N.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 25, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/logging-actions/77252/2 "2006-11-25T17:01:17Z")

</div>

It’s  
controller.controller\_name and  
controller.action\_name

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 25, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/logging-actions/77252/3 "2006-11-25T17:41:13Z")

</div>

Jason N. wrote:

> It’s  
> controller.controller\_name and  
> controller.action\_name

This doesnt seem to work: i get this error

NameError in Backend/productsController#update  
undefined local variable or method `controller’ for  
#Backend::ProductsController:0x4aa78b0

def update  
@product = Product.find(params[:id])  
@product.edit\_counter += 1  
if @product.update\_attributes(params[:product])  
if isLogging()  
createLogFile(@product,controller.action\_name)  
end  
flash[:notice] = ‘Product was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @product  
else  
render :action =\> ‘edit’  
end  
end

def createLogFile(obj,the\_action)  
log = LogFile.new()  
log.the\_class = obj.class.to\_s  
log.the\_class\_id = obj.id  
log.the\_action = the\_action  
if User.backend\_user != nil  
log.user\_name = User.backend\_user.login  
end  
if log.save  
else  
print\_errors(log.errors)  
end  
end

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 25, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/logging-actions/77252/4 "2006-11-25T18:01:23Z")

</div>

Nick B. wrote:

> Jason N. wrote:
> 
> > It’s  
> > controller.controller\_name and  
> > controller.action\_name
> 
> This doesnt seem to work: i get this error
> 
> NameError in Backend/productsController#update  
> undefined local variable or method `controller’ for  
> #Backend::ProductsController:0x4aa78b0

controller.controller\_name is for views. Inside a controller, the  
controller  
is ‘self’.

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 25, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/logging-actions/77252/5 "2006-11-25T18:06:03Z")

</div>

Phlip wrote:

> controller.controller\_name is for views. Inside a controller, the  
> controller  
> is ‘self’.
> 
> –  
> Phlip  
> [Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

Thanks  
It works

N.
