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
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_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.
Below is an example from the Rails API
More about these methods can be found on the Rails API.
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.
Frustration is never good for business.
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.
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.
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
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
attr_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).
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.
The next stesp will be adding the a lot more functionality and I will post on them as I do. I want to add:
- The ability for comments
- Ability to create draft, and future posts
- Contact Form
- Custom sidebar twitter feed
- Post tweets from admin for new post notification
- Login and profiles for guest bloggers
Functionality way further down that line but will be really awesome
- Front-end customization from admin area
- Creation and reorder for dynamic menus (think WordPress drag and drop menus)
- Ability to add and customize modules
- And much more...
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:
- public/assets* (now I did this because I wasn't using any public assets, and didn't want the rouge stylesheets being added)
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.
Before deploying you'll need to precomile your assets with:
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.
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.
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.
You can also follow me on twitter @eileencodes
Thanks for reading!
I just launched this blog and hope to have some informative posts about what I'm learning up soon.
A shout out to the only reasons this blog is running: