Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “controller

Showing Errors in the Logs

I had some errors occurring in my controller, but it wasn’t displaying in my views.  So I had to figure out how to see what was happening since I didn’t see any messages in the logs that were helpful either.

I found the solution in one of the answers here on Stack Overflow.

By adding this to my controller (it was the creation of a model – @user):

logger.warn("====error==========#{@user.error.full_messages.inspect}============")

This recorded the error in my logs (one of my model validations needed to be changed).


Checking Field Values When Posting in RSpec Controller

I was having trouble figuring out how to check that field values posted in RSpec Controller specs.  In models, I will usually do something like:

@foo.reload.name.should eq “bar”

However, that doesn’t work in Controller specs – it doesn’t recognize reload.

In that case, all you have to do is:

assigns[:foo].name.should eq “bar”


Where to put code?

It was confusing for me figuring out where to put logic.  As it turns out, I had way too much code in my controllers and views – this should have been in my models and helpers.

This is a great post:

http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

 

Also, view:

http://stackoverflow.com/questions/60658/rails-model-view-controller-and-helper-what-goes-where


How to Check the Controller

I needed to write a conditional statement checking the controller, I figured out the syntax through this post:

http://stackoverflow.com/questions/5222723/how-do-i-write-a-conditional-to-test-which-page-im-on-in-rails