nerbie69 off rails

Chronicling my experiences with ruby on rails, web application development/management.

Monday, August 31, 2009

Blog Activity - switching blogs

My use of this blog is slowing down. i will emerge at my new blog. In this new blog you'll get all the same rails talk, just without the google/blogger chessiness.

Pre App launch jitters...

I'm going into alpha/beta testing phase of my app. I'm pretty excited but also am kinda nervous.

Whenever you launch a product of any kind, it's human nature to second guess yourself and to ask "why am i doing this?" or "Will this work?". Or the big one, am i ready


Well you are never ready, it'll never be good enough, and you missed something. Big. Shit happens. Be a man and move on.


When you mess up your git pull


Remember this chunk of code:
sudo git reset --hard HEAD

Use that code snippet when you don't care about the changes on the local machine you are git pulling and your git pull request won't merge.

Friday, July 10, 2009

superclass mismatch for class ModelExampleGroup - Solved

If you have been pulling your dick hair out, like i was, i thought i'd post this here, while i'm still in a rage.


Error


When you are running cucumber features, you get the following shit:
cucumber features
superclass mismatch for class ModelExampleGroup (TypeError)...


At first, i commented out the remarkable line in environment.rb. I have no idea what this gem does, And i don't want to know. Hell, I only put it there because of a railscast i think, but this gem is fucking me up, so, again, i commented it out of the environment.rb file and everyone is happy. Good riddance.


FiX?


I just installed remarkable-rails instead. i guess the README in the github repo says how to do it. I've not gotten the error since.

Wednesday, July 1, 2009

30 days of Rails - Handling Flash Messages

So, here i am trying to expand my closed world beyond Restful_authentication and well, basically any other technoweenie based plugin. I try my hand at Clearance


Clearance is a thoughtbot based gem which authenticates users in your web app. Out of the box, it only allows for email addresses as the login ID, which is fine for my stupid app i'm making.


However, in all my years of scaffolding, i guess i've only really used flash[:notice] for displaying my flash messages on an overall layout level. Clearance has flash[:success] and flash[:error].


The reason i found this out is because my clearance_features weren't working and i was getting 8 failures, due to the fact that I was only displaying flash[:notice]. A win for BDD i tell ya.


Gee mister, how'd you fix it


Like all expert coders, i went to the one place I can trust. Twitter. I sent out a tweet which read: twitter ruby fun: lame, hack or on track? < % = flash[:notice] ||flash[:success] || flash[:failure] % >


Most people hated this code, which is fine, as that is what i wanted to hear. While it will work, if i had a need for two flash messages at once, or if i needed to expand my flash message symbols, i'd be fucked. Enter @heycarsten


This dude turned me onto a pretty sweet gist he wrote up for me. A thousand thank you sir's to him.


Gist... Break it down hammer style.


Well everytime you see me, the hammer is on dootie...ok, hammer time is over.



  1. Create a helper called flash_helper.rb and paste this gist into it.
  2. inside the layouts directory, create a folder called partials and a partial inside of your layouts folder called /layouts/partials/flash_message.html.erb.
  3. Paste this into it(stupid stupid blogger's formatting sucks ass... so you'll have to take the spaces out)
    < p id=< % = "flash_#{type}" % > >
    < % = flash[type.to_sym] % >
    < /p >

  4. In your application_controller.html.erb you'll need to add this code to display your new fancy flash messages:
    < % = flash_messages % >

  5. In your css file, now you can create styles with the id flash_whatever. I used #flash_error{background-color: red;}, etc...If you are new to css, learn up on it son.

Yeah for heycarsten and many thanks to him for allowing me to post this code.P.s., i apologize for this shitty code formatting. blogspot sucks ass.

Monday, June 29, 2009

30 days of Rails - Hpricot parsing

Scenario - You need some information off of a website, but there are around 100 rows. This information will also need to populate the name column of your model, so you need to clean up the data. Bascially you need to parse information from a paragraph or one big string. This is where hpricot and some ruby come in handy.


html string parser - Rake task


Here is the code i ended up using, and will try and explain it at the end



namespace :sponsor do
desc "seed data"
task :seed => :environment do
require "open-uri"
require "hpricot"
doc = open("http://www.nascar.com/guides/sponsors/") { |f| Hpricot(f) }
@snag = (doc/"#cnnContentArea").inner_html
@snag.gsub(/<.*>/,'').split(/\n\t+/).map {|t| t.strip }.reject {|t|t=='' }.each { |t| Sponsor.create!(:name => t) }
end
end


Code explained


I needed to pull information from that actual url. All of this information lived within the div tag id cnnContentArea. inside the source of the html page, it was prepopulated with \n\t\t\t and the number of tabs varied.


Using hpricot, we open the url page and save it as an hpricot object (see the doc = line above.


Next, we take the saved page and pull out the content from the cnnContentArea div tag, and we save it as an instance variable @snag.


The last chunk of code, does the following:


  1. The @snag string basically replaces itself without any of the paragraph tags (that what gsub does).
  2. Next, the string is split into mini strings based on a delimiter, and in this case anything that matches \n followed by any number of tabs (\t). I was lucky to be able to do it this way.
  3. We then iterate through each mini string(that's what map is doing) and we then strip all whitespace, before and after the words in the mini string. (see the block t.strip
  4. As an added measure, if the mini array is empty, then it is rejected (courtesy of map)
  5. Lastly, we then take the clean mini string and add it to our model, which in this case is a simple Sponsor model under the name column in the DB.


I have to thank emitilin from IRC as he spruced up my code, but i do take solace in the fact that i knew where to go and now i have an even greater understanding of ruby and rails. I'm sure in the comments you can point me to a method that does this in one fail swoop, but when you are learning ruby, you are a winner when you get to apply the knowledge to a problem you need fixed.

Friday, June 26, 2009

30 days of Rails - Model Associations

You've picked the perfect model names for you new app, but you are unsure when to use a has many :through association or should it be a has and belongs to many association? Or should it be a simple many to many association. Wait, what about has_one and what the hell is a polymorphic anyways?


Well at this point in the game, you may understand what each of the Model Associations do, when you branch out from the basics, or don't use them for awhile, you end up forgetting when and where to use them.


In an effort to streamline the decision of when to use the various Model Associations available in your rails app, I've come up with a decision tree app called Model Association Decider. This decision tree will help you decide when to use a model association in rails. So use this app to determine which association you should use. Model Association Decider

Wednesday, June 24, 2009

30 days of Rails Series - Introduction

Well here i am. I've taken a good look at what i need out of my life, computeristically speaking, and have decided that I am going to sink even more effort into learning Ruby on Rails, the one true and just framework, praise be unto him.


Thirty Days of Rails.. WTF?


What I've decided to do is document, a post a day for a month, about what i've learned in my rails journey from today moving forward.

I will focus on rails methodologies, techniques, optimization strategies, and anything else that i end up learning.


What the 30 days of Rails isn't...


This isn't a beginners class. I won't explain myself as indepth as I would, had I started from scratch. I'm starting with a loose grasp of rails knowledge obtained from a year and a half of using the framework. That means, i know how to do shit, but i want to learn MORE so that i can create apps trouble-free from here on out. Since I've made it my journey, I will pass these tidbits on to you, the gentle reader.


So let's see where this thing ends up. I take it that I'll probably end up with a lot of posts regarding TDD/BDD testing as i know sweet fuck all about it,however, I will post what i do find out, hoping it helps someone down the road.