I can hear the sound now…AC/DC…something about a highway (railway) to hell. Yes its true I’m taking a look at Ruby on Rails, not entirely by choice, but you know what? Learning is good and there’s always something you learn when you learn even if its just what is not worth learning or worth learning. Kinda like watching CNN today and FOX tomorrow…I’m sure I just lost half my readers. Ok so the point is – yes I’m going to fiddle around with Ruby on Rails on macOS to see how it compares to other web frameworks I’m using today (Symfony 3, Node+Express+Sails, and Foundation) or have used in the past.
How We’re Going About Learning Ruby on Rails
Starting with an MBP running the latest macOS (OSX), we’ll start with a homebrew install and follow that up with a good dose of reading on several internet resources. I have access to lynda.com and then have a couple of decent courses on Ruby and Ruby on Rails fundamentals.
Installation with “brew install ruby” works straight off – no issues with that. I’ll be doing all my work with MySQL and I have the latest of that installed. If you have general questions about setting that and just about all other development tools on macOS check out my post here.
As I said I have a burning platform at work (aka a project I need to get done) so I’ll be learning on the fly with a few other folks in the team. Rails is pretty similar to Symfony 3 so luckily I do not think this is going to be a big deal to pick up. There’s also a set of fairly good training on Udacity and on Lynda that I’ll be running thru.
Tips and Tricks for macOS/OS-X
- Install with homebrew (can’t get any easier):
brew install ruby
It will be installed to /usr/local/bin – its a good idea to add this to the head of your path so that ruby and gem will be found in the homebrew location and not get messed up with the default ruby install on macOS.
- Create an app:
rails new my_app -D mysql
- test access to a database with: rails db:schema:dump
- ‘ or ” yes it matters…use the double quotes only when you need processing inside them.
- Spaces seem to matter myFunction( is right and myFunction ( is not right…
- Ruby 1.9 hash syntax – instead of :key => value use key: value
- Useful commands and helpers:
- rails routes
- rails console (dev is default)
rails generate controller MyController index show new edit delete
Ruby on Rails Development Environments
I’m a big fan of Jetbrains tools. They have a Ruby specific IDE called RubyMine. They also have a plugin for folks that license their IntelliJ Ultimate subscription product. You could use things like Sublime and Komodo as they also make a fine Ruby IDE, but I’ll stick with Jetbrains and the IntelliJ plugin. In the IDE there is style formatting and best practices tips, debugging and error detection, and you can run all the typical ruby command line things (like generator commands) right from the “New” option by right-clicking on the project. For example right click and select “run Rails Generator” and in the dialog type Migration and it will give you a helper. Enter a name like “MySillyMigration” and it will run that and create the migration in the right spot in your project.
You can also still do everything from the command line and IntelliJ will pick it up and synch correctly.
Tips on Configuring IntelliJ for Ruby on Rails Development
- Install Ruby as described above and definitely make sure the right version is shown in terminal with “ruby -v”.
- Install the Ruby plugin as you would any other plugin with IntelliJ.
- Create a project – or better yet grab a project from github and import it.
- Once you have a project you will need to configure it (the IDE does not tell you this, rather it will start blabbering errors and things. The key is that the plugin install does not actually setup a Ruby SDK for you. So you will think all is good because high-lighting is mostly working, but really you are not good to go. After getting the plugin installed you will either get prompted by the IDE that there is something wrong or you can click on the “project structure” icon to configure a Ruby SDK. What icon? See the picture to the right! It takes some doing to figure that one out. Click that icon and then go to the SDK section. You will need to add a Ruby SDK (the one you installed with homebrew). To do that enter the Ruby interpreter path: usr/local/bin and the gem path is the same (/usr/local/bin). Magic should happen…and you should be good to go.
- If you are getting prompts for sudo access to install gems then there is something wrong. With a homebrew install you should not get prompted for sudo. This probably means that IntelliJ is not pointed at the right SDK.
- You may get gems installed in your home directory under ~/.gems/ruby/version/gems so you may need to add this to the project configuration dialog under libraries.
Here’s a gallery with additional screen captures:
Tips and Tricks – More to Come…
I do not have a big list here, but you gotta start somewhere:
- If you want to use the link_to helper and also have an image embedded in the link, its not overly intuitive how to do that. Basically use the link_to function, but append the text for the link to the image_tag output and you will be good to go.
<%= link_to(image_tag('plus_sign.png', size:'11x11', alt: '+') + " Link to Something", new_something_path, class: 'action new') %>
- Keep code out of controllers. Models and Helpers are great places for code.
- All in or all out. With a framework like RoR you need to decide this one and IMHO, you need to be all-in and embrace the framework or go do something else.
Impressions of Ruby on Rails
I’ve played with a ton of frameworks over the years. RoR is fairly similar to Symfony 3 – yeah don’t laugh. They have a lot of the same automation capabilities in terms of project creation, database manipulation, dependency managers, etc. As with Symfony which has been around for years, one of the nice RoR features is the community. If you can’t figure it out, most likely someone else has. However as with Symfony you need to be careful to when reading up that the solution works on the version you are using.
- Routing is really nice – intuitive. Resource routing is the way to go. The MVC implementation is pretty clean, but you do need to get used to where everything is and what all the helper functions are.
- In general the setup is pretty easy with controllers and views and naming conventions. The file and class naming conventions are important so be sure to use the generator tool and be sure to remember the rules.
- There are some annoying things with single and double quotes, when spaces seem to matter, etc. that are a bit annoying.
- There are lots of minor changes to syntax that don’t get much play in the community. Here’s a good example.IntelliJ does a decent job of warning about using the newer formats.
- The rails console is pretty interesting. I don’t think I’ve seen something like this in another framework. Being able to test and work with your model classes and actually persist things (or delete) them directly from your code is really helpful and potentially powerful. The generator stuff is pretty nice too; automates creating a lot of boilerplate code. Downside is that you need to remember things.
- There are lots of annoying syntactical things to remember – “<<” to append things, various uses for the : operator, the single vs double quotes, declaration of arrays with strings [‘string_one’, ‘string_two’] vs. %W(Create an array of strings from these words). I guess these are handy for people totally committed to rails, but make reading and learning code difficult.
- It reminds me a lot of Cold Fusion (remember Allaire?) – powerful, easy to do many things, but lots of things to remember. You would really need to commit to the entire framework to be good with it.
I can certainly say that I’ve found it fun to work with and learn. I’m not sure how much I’ll end up doing with Ruby on Rails, but if I do I’ll keep adding up tips and tricks that I learn here.