Over a million developers have joined DZone.

TextMate Snippet For Load_model Methods In Rails Controllers

·
With nested routes, I find I create a lot of controller methods like:


def load_user
  @user = User.find(params[:user_id]) if params[:user_id]


Here's a TextMate snippet, so you can just type: defmodel, TAB, user, TAB, and you're done.

Snippet:

def load_${1:model}
	@$1 = ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g}.find(params[${2::$1_}id])${3: if params[:$1_id]}
end


Activation: defmodel
Scope: source.ruby.rails 

I have this in the Ruby on Rails bundle, but you can put it anywhere. Its the scope that is important.
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}