Week 2 Round Up: say-anything

Week 2 of An App Per Week is over! It was tough and I got a little behind. I planned to implement the RailsBridge intermediate curriculum, plus testing and some extra features. Here is what I was able to accomplish:

What I wasn't able to finish this time around: And here it is! All in all, I am pretty happy with it. The major things I learned:

Unsolved problems:

When testing, I had to run some tests as a signed in user and some as signed out. How do I do this? After some reading, I figured out that I had to stub the authentication in the specs. I followed these directions and put the ControllerHelpers module in spec/support/controller_helpers.rb as well as the accompanying config. But it kept complaining: "undefined method sign_in".

After a certain amount of googling, head scratching, and generally going nowhere, I finally just moved the module into my spec file. Not graceful, but I had to keep going. If you have any ideas about this let me know.

Also while trying to test user authentication related things, I figured out how to fake sign in/out inside of rails console and get to the user object:

ApplicationController.allow_forgery_protection = false
app.post('/users/sign_in', {"user"=> {"username"=> "jimmy", "password"=> "mypassword"}})
cu = app.controller.current_user

I find it super useful to be able to try things out in the console. It's probably where I learn the most.

Another problem. In Michael Hartl's Rails Tutorial book, he was able to set up a has_many / belongs_to association between User and Micropost. And because this was set up, he was automatically able to write something like this:


However, I set up my app in a very similar way. My :post belongs_to :user and my :user has_many :posts. But I couldn't get this to work. I always got "undefined method posts". My workaround was simply to use plain old Posts object and pass it the current_user when needed.

Another thing I learned... where and find_by_??? methods are not interchangeable:

x = User.where(id: 3) # returns ActiveRecord::Relation
y = User.find_by_id(3) # returns User
x.id # error, no id method
y.id # returns the user id correctly
x.each { block } # works
y.each { block } # error, no each method on User

There are many other smaller things I could blog about, but I don't have time and you probably don't have patience. I may write up a few of these smaller issues later if I feel like it.

Next Week

Any ideas? I'm thinking about writing an app that would involve pulling from some API... maybe the Twitter API and doing something with the data. Stay tuned.