Close and Go BackBack to Viget

Ruby on Rails

Paperclip Custom Interpolations

Brian Landau
Brian Landau, Web Developer, January 13, 2011

As web developers something we often have to implement is handling file uploads. With Rails there are a number of plugins and gems for helping with this. The one I use most often is Paperclip. Out of the box, Paperclip does exactly what you want, and you don’t have to think about any configuration. But, as your application matures, your needs often change and some custom configuration is often required. One thing you’ll notice is that you can customize where the file is stored. The way that you do this (for those new to Paperclip) is:

has_attached_file :asset, :url => "/system/uploads/:class/:attachment/:id/:basename_:style.:extension"

On a recent project the client wanted us to store uploads in a very specific path structure. On the surface this seemed a simple enough request. As we dug deeper, though, it became clear that one part of the path needed to be based on the attribute of a model that was related to the model on which the file data was being stored. By default Paperclip only allows a limited set of values to be used in the path: filename, timestamp, rails_env, class, basename, extension, id, fingerprint, id_partition, attachment, and style. These predefined interpolations are all you need most of the time, but our case was the exception.

Continue reading "Paperclip Custom Interpolations"

Rails 3 Generators: Scaffolding

Ben Scofield
Ben Scofield, Former Staffer, March 02, 2010

Scaffolding in Rails has always been a little controversial. Originally, it was responsible for a lot of Rails’s wow factor – DHH’s blog in fifteen minutes was built on scaffolding. Over time, however, it became less clear whether it was supposed to be used for production code, or if it was intended to educate new Rails developers on best practices.

After a few discussions through various channels, the core team eventually settled on the latter – scaffolding was supposed to be educational, illustrating the best practices around RESTful controllers and other Rails conventions. Even with that goal, however, there’s still been some difficulty around updating the scaffolded views to teach people how best to, for instance, split files into partials.

Continue reading "Rails 3 Generators: Scaffolding"

Rails 3 Generators: Say Hello!

Ben Scofield
Ben Scofield, Former Staffer, February 25, 2010

Still with me after the last batch of ported generators? Well, I’ve got something new for you this time – and by new, I mean unseen by Rails 2.3.5-looking eyes. That’s right, it’s time to take a look at the generators that make their first appearance in Rails 3!

Continue reading "Rails 3 Generators: Say Hello!"

Rails 3 Generators: The Unusuals (part 2)

Ben Scofield
Ben Scofield, Former Staffer, February 24, 2010

After the first two installments of the Rails 3 generator tour, you may be asking yourself, “Hey, what about session_migration? Or performance_test? I use that one all the time!” Well, wait no longer! Here are four more of the generators that have been held over from Rails 2.

Continue reading "Rails 3 Generators: The Unusuals (part 2)"

Rails 3 Generators: The Unusuals (part 1)

Ben Scofield
Ben Scofield, Former Staffer, February 19, 2010

OK, we’ve seen how the most common generators have changed (some more than others) in Rails 3. Let’s look at a few more that’ve been around for a while, but haven’t been used quite as much.

Continue reading "Rails 3 Generators: The Unusuals (part 1)"

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Upcoming Events

Future Insights Live - April 29 to May 4
Future of Web Apps, Future of Web Design, Future of Mobile, and Future of Web in the Enterprise have joined forces to bring you our most comprehensive event EVER!

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


How many minutes in an hour?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.