Category Archives: Controllers

Nokogiri and the Ruby SAML Toolkit

I ran into a problem today while working with the Ruby SAMLToolkit, an open source project by the OneLogin Identity Provider. This toolkit offers a relatively easy way to implement SAML authentication into your Rails application. Ratified in 2005 as an OASIS standard, SAML is definitely a great protocol to use for single sign on solutions in your application.

Since SAML is an XML based protocol, I’m sure you can image that in the Rails world, the nokogiri gem would prove useful in implementing SAML single sign on inside a rails app, since nokogiri enables robust document parsing, which is needed for the XML used by SAML.

And sure enough, the Ruby SAML Toolkit utilizes nokogiri!

But there’s a gotcha here. Nokogiri has a Nokogiri::XML module with a few constants: XML_C14N_1_0, XML_C14N_EXCLUSIVE_1_0, and. XML_C14N_1_1. These constants are utilized depending on the standard serialization of the SAML XML being processed during the single sign on handshakes between your application and the identity provider. Continue reading

Tagged , , , , ,

How to Skip Filters in Controller RSpec Tests

I ran into an interesting dilemma today with a controller spec.

I needed to verify that a post to my controller’s update method did indeed update a particular setting on an instance of one of my models. Unfortunately, the controller I was using inherited some before_filter(s) from its parent that called a method that checked for a logged in user.

But all I wanted was to make sure my model’s setting actually got changed!

Continue reading

Tagged , , ,

Why it’s Bad to Pass OpenStruct (or Object) to Controller from View

I recently ran across an instance where I needed to pass a relatively simple OpenStruct from a view back to the controller via a GET. It was a strange situation where I needed the OpenStruct to populate some tables in the view, but upon a user submit, needed to use that same OpenStruct back in a controller action.

I found a bad way to do it.

 

Continue reading

Tagged ,

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

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 , , ,