Could Not Parse Yaml, Why!?

Yaml config files are a great way to store static data in your rails application. But there’s a little caveat I experienced today. Observe the following yaml file:

dog:
  poodle  
  husky  
  "golden retriever"

Looks fine, right? Now let’s assume you’ve named this yaml file dog.yml and it lives in your application’s config directory. You’d figure you could access the data by doing something like the following:

config=YAML.load_file('dog.yml')
puts config (yields the hash {"dog" => { "poodle", "husky", "golden retriever" } }

But for whatever reason, you are getting an error (couldn’t parse YAML at line x column x). You suspected that “golden retriever” might be a problem due to the space, but you’ve already quoted it, so no, it can’t be the problem.

Continue reading

Advertisements
Tagged

Basic Controller Rspecs

MyWayOnRails

Suppose the following method is there in the controller

def new
  @post = Post.new
end

For this the corresponding spec will be as follows

describe "GET #new" do
  it "creates new instance of post" do
    get :new
    assigns(:post).should be_a_new(Post)
  end
end

In the above code “get :new” line will get the new path of the controller and execute the corresponding method described in the controller. So that particular statement is very important for execution of the method and comparison of the output in the test.

View original post 458 more words

Auto Generate Factories for your Models

My good friend and colleague DS filled me in on a little trick that I’d like to share. When testing, it’s a good idea to have factories written for each model. Now, if your model is already created, you’ll have to spin up that factory manually. But how about ensuring your factories are created with each new model you generate?

Enters this nifty bit of code you can add to your config/application.rb file, within the Application class:

Continue reading

Name Those Classes Properly

Although it may not seem intuitive, Rails is very picky when it comes to how you name your files, specifically  when adding new classes within your app folder.

Let’s say you have a class that looks like this:

class MySuperCatHelper

   def meow
      puts "Meow!!"
   end

end

You’ve saved this class in the app/helpers directory in a file called my_supercat_helper.rb. Naturally, you want this class to be available to your MySuperCat controller where you wish to add the following code:

cat = MySuperCatHelper.new
cat.meow

But for some reason, you’re getting an error about an uninitialized constant when using the above code in your MySuperCat controller.

Why is that?

Continue reading

Rake Tasks

With any web application, you will find yourself needing to run ‘behind-the-scenes’ tasks. In the .NET world, I often found myself writing scheduled tasks to run on app servers, or utilizing Taskie to accomplish these goals. But with Ruby on Rails, you’ll find yourself writing Rake tasks.

Continue reading

Tagged , ,

Rails pagination, will_paginate, and AJAX

One of the beautiful elements of Ruby on Rails is how much you can do with so few lines of code. The will_paginate gem is a prime example. With just a few line’s of code, you can add pagination to any of your application’s lists. Having dealt with home brewed pagination methods many times during my years as a web developer, I can only say that this little gem has brought me much joy. Below is an example on how to use it.
Continue reading

Tagged , , ,

Responsive Design

My wife, daughter and I visited a friend/coworker over the weekend. We had a wonderful time getting to hang out with him and his wife, letting our nine month old roam their 1850’s plush carpeted farmhouse, and exploring the ancient basement, home to a toad, cobwebs, and chiseled stone walls.

But the day would not have been complete without my friend and I sitting down and coding some Ruby on Rails.

As he and I discussed the apps we’d been working on, I showed him a little about the Rails asset pipeline (will post about this in the future), while he showed me how he’d used a nifty css/javascript framework to set up responsive design for his site.

Continue reading

Tagged , ,

Critical Rails Security Issue

In January 2013, I was informed from a colleague about a severely critical security hole in nearly all Ruby on Rails versions. Although I’d read about the issue a couple weeks prior, I didn’t think I had to immediately worry about making the necessary upgrades to the patched versions, because all the Rails applications I was currently working on were still only in development modes, and running on local workstations under localhost. But then my colleague sent me a link to the following article (if you develop Rails applications, please read it):

http://www.kalzumeus.com/2013/01/31/what-the-rails-security-issue-means-for-your-startup/

Hopefully, you noticed from the article that even development applications running under localhost are vulnerable to this security breach. Continue reading

Tagged , ,

Rails Mass Assignment Error/Security

When creating a form in Ruby on Rails, you’re offering an end user a way to save data to your database. But that end user could be anyone. It could be someone who is using your site as intended, or, someone who is trying to hack into your system and ruin your life.

Continue reading

Tagged , ,

Vagrant Virtual Machine with Puppet for Rails

When I first started trying to learn Ruby on Rails, I noticed that there seemed to be a lot of steps, apt-gets, and other hoops and obstacles I had to tackle prior to getting my first “rails server” command to successfully fire up localhost:3000 with my rails application. I remember thinking to myself, if this is how hard it is to set up a workstation to run rails, then I’m not too sure it’s going to be all that pleasant to work with.

In particular, I realized that in a team setting, having to configure multiple workstations for each developer would be less than awesome. Yuck!

Enter VirtualBox and Vagrant. VirtualBox is a free Oracle product that allows developers to add virtual machine instances to their local operating system install. Vagrant is essentially a way to download an instance of an operating system, such as Precise32, and fire up a VirtualBox virtual machine on the fly in accordance to a specified config file.

Continue reading

Tagged , , ,
Advertisements