What is New Relic?

New Relic is a performance management system that our company just started using to monitor some of our larger apps. Since this app occasionally drops mysql connection (I'm thinking it's related to Unicorn) we're using it as a test case.

By using New Relic you can get real-time browser performance, app performance, database performance, email notifications. And on top of being a great resource it looks awesome. Some really nice design there.

Installation and Use

Installation for Rails Apps is easy. Add the gem to your Gemfile inside your production group.

gem 'newrelic_rpm'

When you're done with that add newrelic.yml Inside your config/ folder inside your app. You can get the default settings from the github project.

You'll need a license key and to change your group's based on your configurations if you don't want the default options.

Once deployed New Relic automatically recognizes your Rails App and begins monitoring it. It monitors who is visiting your site and from what country as well as all the processes it is running. Definitely worth checking out.

I have for some time been wanting to make a "definitive list of awesome rails methods". There are so many great built in methods that I didn't realize existed when I was trying to build something from scratch. Sometimes it's annoying to find out that thing you're trying to build already exists - but if you haven't put 500 hours into it, it's usually a relief.

So today the featured definitve list of awesome rails methods are link_to_if and link_to_unless.

The nice thing about this method is it's great for changing links based on if a user is logged in, if the page is current, etc. Instead of writing an if else statement you can DRY it up by using link_to_if or link_to_unless. I found it fun to play around with and very useful as well. I personally used it for a complicated if else to mark a category for a prodcut as current. There is also an link_to_unless_current which I didn't end up using because it wouldn't mark both the current category and parent category as active, link_to_if worked much better for that.

This post is not really going to be Rails related. But everything in a Rails blog doesn't need to be about Rails, or Ruby, or coding at all.

I feel like Rails is an experience, in not being frustrated with everything and it's easier to map out an application. The things that get in the way of building something awesome, the time consuming, awful things like join queries and pluralization are done for you. You can focus on your application and it's beauty and structure.

Well, user experience is also an experience and I hate that it's so often that a website has me banging my head against a wall. Recently, I logged on the Sallie Mae website to check something with my student loan. Being that I have it set for auto-debit and that I was logging in on a weekend I was met with a front page telling me I was delinquent on my loan.

Now people who know me will know my reaction without telling you. I literally flipped a shit. Thinking my bank account information was lost or some other awful scenario (was my bank account hacked and emptied?? no...it wasn't phew) I searched through my account for an answer when the answer occurred to me.

Sallie Mae does not have a failsafe in place so that when you have auto-debit activated and the date of debit falls on the weekend it doesn't "figure it out" it instead tells you you didn't pay and that you better do it now.

Awesome. Please when building an application make sure you put in if else statements that check for things like weekends, and auto-debit. It will make your customers happy. No one wants a mini heart attack on Sunday evening, to log in the next day and see all is well.

User experience is more than making a site easy to use, and beautiful (both which Sallie Mae fail at). It's also about not scaring the crap out of your clients and consumers — or accepting 76 early acceptance students accidentally (I'm looking at you Vassar). When desiging a web application consider all the horrible things that can go wrong for the user, not just you the developer and put failsafes in place to prevent those things. You will have happier clients that will keep coming back again and again.

Recently I needed to add "forgot your password?" to a Rails app built by my company. I had never done this before so I watched the Remember Me & Reset Password Railscasts screencast. It was infinitely helpful, but I added a few things that were not in the video that I would like to share here. I followed the screencast for the most part: Adding a password_resets controller, in addition to following the auth_token directions. One thing I added was an intermittent page, instructing the user to check their email. I used the index.html.hamllayout for this. In addition I did not like it that users who hadn't requested a new password could access these pages, so I created a few redirects to prevent that. My index and new action show this redirect protection.

If a user attemps to go to the password_resets while already logged in they will be redirected to their profile, if they try to get there without being logged in they will be redirected to the login page, same with the new action.

# index and new action in my password resets controllerdefindexifcurrent_userredirect_toaccount_urlelseredirect_to'/login'endenddefnewifcurrent_userredirect_toaccount_urlendend

I added the functionality to check whether the user's email was in the database and to return an error if it was invalid.

On the create action I have the page rendering the index instead of redirecting to maintain access to the email address so the view can display "An email has been sent to "insert email address". You must click the link in the email to reset your password."

Lastly, I wanted to make it more secure than the token expiring in 2 hours, so I added code that make the token expire if the user successfully changed their password. Once the user successfully changes their password reset_pass_token will be erased from the database and will be redirected to their account profile, so they can edit other settings if they like otherwise, they recieve errors. If the new password save isn't successful within 2 hours the token will also expire.

defedit@user=User.find_by_reset_pass_token(params[:id])if@user.nil?redirect_to'/login',:alert=>'Password reset does not exist.'elsif@user.reset_pass_expiration<2.hours.agoredirect_to'/login ',:alert=>"Password reset has expired."endenddefupdate@user=User.find_by_reset_pass_token!(params[:id])if@user.update_attributes(params[:user])@user.reset_pass_token=nil@user.save!# if params are updated create session for user by matching token and passif@user.valid?session[:user_id]=@user.idredirect_toaccount_url,:notice=>"Your password has been reset!"elserender'update'endelseflash.now[:error]=@user.errors.full_messagesrender:action=>"edit"endend

The setup for "Forgot your password?" was rather easy and I learned a lot from implementing the functionality.

Around the beginning of the year I decided to watch every Railscast tutorial. I know what you're thinking — that this was a "New Year's Resolution". Well that's simply not true. It just appears to be one. I resolved to do that way before the New Year, it just so happened that on January 3rd is when I had time to start.

Well some people have said, why watch every one? They are old and from 2007 and some aren't relevant anymore. That may be true, but at EMN we have some legacy Rails sites that I didn't build but might have to one day fix, and it helps to know what has changed from Rails 2 to Rails 3, etc.

I've also learned some really great things and tips. One in particular is the footnote plugin for textmate which links the error messages in Rails to where they appear in your code. In your footer it also describes the SQl calls & queries, and analyzes the speed, shows the CSS and HTML and everything you might want to inspect for that code. The video that describes it is "The Stack Trace" and although the method for installing the plugin shown in the video no longer works you can download it from github and follow the installation instructions there. It needs to be installed for each app and added to your .gitignore because this plugin should only be used in development mode.

I've been working on this blog in bits and pieces, when I have time. I've added some new functionality to bring it closer to a real blog CMS.

Adding Published at date changer

First I added a Published at date changer so that I could change my post dates if necessary. I think I'm going to do a "definitive list of RoR helper methods" because if it weren't for my pledge to watch all the railscasts videos in under a year I wouldn't have know about date_select and datetime_select and I would have been putting my head through a wall trying to figure it out. I feels almost like Rails cares about my sanity!

To use this helper method make :created_atattr_accessiblein your Post model and then add the following to your admin view:

Belongs_to user for posts

I figured in the future I may have some guest bloggers, and it would be nice now to have all the posts have an owner so I woundn't need to go back and add it later.

First I created a migration so that would add the current users ID to the post table. Then I updated the post and user model adding belongs_to :user to the post model and has_many :posts to the user model.

Next, I wrote a method so I could call it in multiple actions if I needed to (for at least the first three posts I wanted to call the method on update to add the user to the table, also I only have one user so I knew nothing would happen).

classAdmin::PostsController<Admin::AdminController#only showing the update methoddefupdate@post=Post.find(params[:id])if@post.valid?add_author_to_postsave_or_remove_post_categorizationendrespond_todo|format|if@post.update_attributes(params[:post])format.html{redirect_to[:edit_admin_post],:notice=>'Post was successfully updated.'}elseformat.html{render:action=>"edit"}endendend#method created for adding user to posts tabledefadd_author_to_post@user=User.find_by_id(session[:user_id])@user.posts<<@postendend

In my view I was getting my most hated errror "You have a nil object when you didn't expect it". Yea Rails, if I expected it we wouldn't be haivng this conversation...

Anyway, I quickly figured it out was because I did not already have my author filled in so of course rails was complaining. Here is my view for the author section.

Note: I never did finish parts 2-4 for this blog post. I waited too long to write them and forgot how they worked. I've left this post up even though it's not really valid any longer. There are much better articles now on getting this running.

I'm going to do this post in four parts. Part 1: checklist things to do before deployment, Part 2 Capistrano, Part 3: Unicorn, and Part 4: Nginx. I also don't have the ability for draft posts set up yet, so...instead I'm publishing in parts so I don't need to write it all at once.

Launching this Rails app was not easy, although since I had never deployed an app, I wasn't expecting it to be easy. There are a bunch of little things you need to know about deployment if it's your first time that I think it would be helpful for other first-timers. Also, when you've been working on that web app for 24 hours straight you forget the all important mental checklist.

Please feel free to correct me to tell me I'm "doing it wrong" because this is my first deployment and I will fully admit I might have no idea what I'm doing.

Preparing Your App for Deployment

One thing I failed to do and I regret a lot is making sure certain files aren't include in git with my .gitignore. Files to remove are:

db/*sqlite3

log/*.log

public/assets* (now I did this because I wasn't using any public assets, and didn't want the rouge stylesheets being added)

Gemfile.lock

Chances are if your app is running on sqlite3 in development it won't be in production and will more likely be using a MySQL database or PostgreSQL. My app is using MySQL so we'll go over setting up your Gemfile for that.

If you have just gem 'sqlite3' in your Gemfile you'll need to change it to what I have below. While here you'll probably want to add the gems needed for deployment as well.

group:developmentdogem'sqlite3'endgroup:productiondogem'mysql'end# Use unicorn as the web servergem'unicorn'# Deploy with Capistranogem'capistrano'gem'capistrano-unicorn'

Before deploying you'll need to precomile your assets with:

rake assets:precompile

This is really important because I forgot this step and could not figure out why my assets weren't showing up and I was getting a lot of "No Route Matches" in my logs. I didn't find this anywhere other were having this problem and no one said "hey maybe you forgot this really important thing."

Once capistrano is set up you can automate this task during deployment, but we'll cover that in Part 2.

When Parts 2-4 are written they will be linked to at the bottom of this post.

An Introduction

First, Happy New Year to everyone. This blog isn't a New Year's Resolution, so don't worry I won't write in it for 15 days and then never appear online again.

I started this blog for a couple reasons. I went to Big Nerd Ranch in September to learn the basics of Ruby and Ruby on Rails, and in that class we learned one of the ways we could contribute to the Rails community was writing a blog. I wanted a way to document and collect the things I learned, the mistakes I made, and also hope to help other beginners.

It will also help me hold myself accountable - to always be learning new things about Rails and giving back to the open source community.

From Photographer to Developer

When I thinking about how I got where I am now I can't help but laugh a little. I can tell you that if you asked me where I'd be now 5 years ago, I wouldn't have said where I am now. In high school, I focused a lot on art classes and thought perhaps I wanted to be an art educator and went to college believing that's what my major would be.

I think I was about 5 seconds into my freshman year when I realized teaching art was not something I wanted to do. I decided on a photography major and although I wish I had realized my passion for web development and computer science much earlier on I am grateful for all the creative energy I was required to use during my time as a photography major. I think I learned some valuable things that can easily be applied to where I am today, for example, the importance of maintaining the scope of a project.

In my senior year of college I took a Flash class for two reasons; it fit well in my schedule and I wanted to learn how to build a basic portfolio site for myself. As our final project I built my original website. At first my professor discouraged me from doing this because with only one month to complete the project he felt the timeline was a little to ambitious. I strive to constantly challenge myself. and I did in fact finish it ontime - with lots of hours over Thanksgiving break.

That was 3? 4? years ago I lose track. I took another class or two, but most of what I learned was on the job or things I taught myself. I have learned so much in a few years I cannot believe the information I absorbed.

For the last two and a half years I have been working for Evolving Media Network, a web development company in New York. Starting out with static html websites I quickly taught myself enough PHP to hack together WordPress sites and that was the majority of my responsibilities for a few years.

The Present

This past September I went to the Big Nerd Ranch in Atlanta to attend their Beginning Ruby and Ruby on Rails class. It was awesome and if you're thinking of going it's a great learning environment and the instructors are talented and helpful. Since then I have been practicing Rails by working on my own projects and have a few ideas for gems I hope to get to soon.

About

Hi, I'm Eileen! I'm a Senior Systems Engineer at GitHub. I'm an avid contributor to Open Source and am on the Rails Core Team and the Rails Security Team. I enjoy speaking at conferences, usually about performance or contributing to Open Source. Other than writing code, I enjoy craft beer and hiking with my husband, Abe and our dog, Arya.