Rethinking Rails 3 Controllers and Routes | Free PeepCode Blog
The Rails router has been written and rewritten at least four times2, including a recent rewrite for the upcoming Rails 3. The syntax is now more concise.<br />
<br />
But never mind making it shorter! It’s time for a final rewrite: Let’s get rid of it altogether!
Related cards:
Nested controller routes (Rails 2 and 3)
In order to keep the controllers directory tidy, we recently started to namespace controllers. With the :controller
option you can tell Rails which controller to use for a path or resource. For nested resources, Rails will determine the view pat...
Security issues with hash conditions in Rails 2 and Rails 3
Find conditions for scopes can be given either as an array (:conditions => ['state = ?', 'draft']
) or a hash (:conditions => { 'state' => 'draft' }
). The later is nicer to read, but has horrible security implications in some versions of Ru...
How to create Rails Generators (Rails 3 and above)
General
- Programatically invoke Rails generators
-
Require the generator, instantiate it and invoke it (because generators are
Thor::Group
s, you need to invoke them withinvoke_all
). Example:require 'generators/wheelie/haml...
iPhone on Rails and ObjectiveResource; Making communication between the iPhone and a Rails web-service pain-free.
ObjectiveResource is an Objective-C port of Ruby on Rails' ActiveResource. It provides a way to serialize objects to and from Rails' standard RESTful web-services (via XML or JSON) and handles much of the complexity involved with invoking web-serv...
Rails routes: Extracting collection actions into their own controllers
Let's say you have two screens:
- Show a given project
- Show a report for all projects
Ideally you want both screens to be handled by different controllers like this:
GET /projects/:id => ProjectsController#show
GET /projects/...
Make Sure Your Rails Application is Actually Caching (and not just pretending) - Alfa Jango Blog » Blog Archive
It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching.
Rails 3: Mass assignment protection and .create_with
The issue
Yesterday, Rails fixed a security issue (CVE-2014-3514) in Rails 4+. It was possible to use .where
or .create_with
to bypass Rails' [Strong Param...
Use different code for Rails 2 and Rails 3
When writing a piece of reusable code, you sometimes need to have separate code for Rails 2 and Rails 3. You can distinguish between Rails versions like this:
if Rails.version < '3' # mind the quotes
# Rails 2 code goes here
else
...
Rails 3 routing: Be careful with matching routes *including their format*
Today, this line made me trouble. Can you spot the mistake?
match 'sitemap.xml' => 'feeds#sitemap', :constraints => { :format => 'xml' }, :as => 'sitemap'
The mistake is to match sitemap.xml
. Rails will by default strip any dot-anythin...
Save ActiveRecord models without callbacks or validations (in Rails 2 and Rails 3)
Rails 2
You can use
record.send(:update_without_callbacks)
or
record.send(:create_without_callbacks)
This can be used as a lightweight alternative to machinist's make
or FactoryGirl's create
, when you just need objects...