Politics, Programming and Possibilities
17 Feb
Add a shell script to TextMate that moves you from a controller file to its corresponding view (.rhtml) file in one keypress.
See the syncPeople on Rails Bundle for even more features and a much simpler installation.
One of the most inconvenient things about the Rails directory structure is the relative distance between the controller and its corresponding view files. While it’s organizationally sound, it’s often a pain to go hunting for a view (especially in a complicated application that has modules and modules within modules).
I discovered a fantastic little key-combo within TextMate the other day that almost did what I wanted–if you hit command-option-down, you will be taken from the controller to the corresponding functional test (or from a model to the corresponding unit test). Try it out, it’s really cool.
But what I wanted was ease of navigation from my controllers to my views and vice versa. From what I could tell, that’s not a built-in feature of the Rails or Ruby TextMate command bundles. So I wrote one and bound it to command-option-up. Read on to add the command yourself.
Put the rails-open-view-partner.rb file in to the following directory:
~/Library/Application Support/TextMate/ \
Bundles/Rails.tmbundle/Support/bin
Next, in TextMate go to the Automation -> Run Command -> Edit Commands menu (ctrl-option-command-c). Navigate to the Rails commands and add a command by clicking the ” ” sign at the bottom of the window. Call this new command something like “Open Controller / View”. It should be next to the “Open Test Case” command which is the other command I mentioned above.
In the Command(s) window on the right, add this code:
rails-open-view-partner.rb "$TM_FILEPATH" &>/dev/null &
For “Input” choose “none”, and for “Output” choose “Discard”. For “Activation”, choose “Key Equivalent” and type a key combination in to the input box next to it (I chose command-option-up, since it matches the command-option-down quite nicely).
Finally, for Scope Selector, type “source.ruby.rails, text.html.ruby”. Whew. You’re done.
Note: As we’ve discovered in the comments, it’s important to make this file executable by adding the permission: “chmod u x rails-open-view-partner.rb”. Also remember that the scoping rules for this command apply to Rails mode not just Ruby mode.
Now that you’ve got that all set up, try it out. Go to a controller, and within that controller move your cursor inside of some action (e.g. between “def new” and “end”). Hit command-option-up and you’ll be taken to new.rhtml. From within new.rhtml, hit command-option-up again, and you’ll be taken right back to the controller.
Justin Forder pointed out that the RadRails team is looking to do something similar. Kyle Shank has posted about this very same issue and it looks like they’re going to add a quick shortcut command too. Nice work, guys!
12 Responses for "Controller to View and Back Again (in TextMate)"
Thanks Duane! A very handy addition indeed.
One note: I had to give the file execute permissions before it would work.
Hmm, cool but I can’t get this to work.
When I copied your .rb file into the Rails.tmbundle I had to create the Support/bin directories as they didn’t pre-exists. Was this correct?
When I run the command (via the keyboard shortcut) nothing happens.
Also that keyboard shortcut already exists for “Navigation > goto header/source”. I tried other combinations but still no luck.
Any ideas? Would be cool to get this working as it’s rather nifty.
Cheers
Thanks for this!
Just an extra note, after downloading rails-open-view-partner.rb, make sure it’s executable (chmod u+x rails-open-view-partner.rb), otherwise it won’t work.
How do you get Cmd-Option-Up to not conflict with Navigation > Go to Header/Source?
Never mind - the .rb file I was trying in TextMate didn’t think was rails, so it wasn’t even trying it.
Great tip - thanks!
Thanks to everyone for pointing out the two pointers that I missed: the rb file must be executable, and you must be in Rails mode (not just Ruby mode) for the scoping to work.
I chose the command-option-up key combo in spite of that combo being already bound to the “go to header/source” because the command seems almost useless for Ruby development, let alone Rails development. In any event, it just seemed to mirror the command-option-down key combo nicely, so that’s my preference.
Thanks to those who’ve posted instructions for success!
Bingo. Making it executable works for me.
Nice work duane. That always bothers me too. I’m going to have to make several of these for jumping down into the public section as well. Jump to stylesheets, jump to javascript. for example.
Jump to view/controller in TextMate…
Duane came up with a way to jump to the controller method for the view you…
Mailers also have views!
So …
[...] After introducing the controller-to-view textmate shortcut yesterday, my coworker (Pete Lasko) and I got excited about the possibility of expanding this idea. So from some of our ideas I built the “syncPeople on Rails” bundle for TextMate, in honour of the company we both work for. [...]
Leave a reply