Created
March 18, 2011 03:34
-
-
Save d3r1v3d/875577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CarsController < ApplicationController | |
# ... | |
# GET /cars/1 | |
def show | |
@car = Car.find(params[:id]) | |
# ... | |
end | |
# GET /cars/1/edit | |
def edit | |
@car = Car.find(params[:id]) | |
# ... | |
end | |
# ... and so on ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initialize_vars_from_param_ids | |
params.keys.each do |param_key| | |
model_klass_name = case param_key | |
when 'id' then | |
self.class.name.gsub(/Controller$/, '').singularize | |
else | |
next unless param_key =~ /(.+)_id$/ | |
$1.singularize.camelize | |
end | |
model_klass = model_klass_name.constantize rescue next | |
self.instance_variable_set( | |
"@#{model_klass_name.tableize.singularize}", | |
model_klass.find(params[param_key]) | |
) if model_klass.respond_to?(:find) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment