Archive for the ‘Web Dev’ Category
Setting up Ruby on Rails in OS X
I consulted quite a few sources online to try and figure out how to do this, and ran into a few problems that took entirely too much time to solve. Now that I’ve figured them out, I’m writing the solutions here in the event I need them again. Hopefully they’ll be useful to a few other people as well.
Goals
Create a Ruby on Rails development environment with Apache, MySQL, and PHP.
There are several possible ways of doing this, but since OS X is a Unix-based platform, I don’t see any reason to use pre-compiled packages like MacPorts and replacing the built-in software. Plus, this is supposed to be a learning experience.
Sources
http://maestric.com/doc/mac/apache_php_mysql_snow_leopard
http://michaelgracie.com/2009/09/23/plugging-mcrypt-into-php-on-mac-os-x-snow-leopard-10.6.1/
Counting records by category [RoR]
In my Rails application, I have an inventory of items described by three models: the items (cryovials), the place they’re stored (dewar), and the information about which vials is stored where (ln2_locations).
The reason I used three models, rather than rolling the attributes of the third (ln2_locations) into the actual item was that in reality, we normally freeze down multiple vials with the same content at the same time. I would like to implement this kind of mass assignment at a later time.
Anyway, it is often useful to be able to determine what we have stored, and how much of each different type of content is present. It’s one thing to know what the SQL code would be to retrieve these records; it’s another to convert that to an ActiveRecord query when you’re just learning.
What I would like to do is look at my items (cryovials), and pull out the unique content values (e.g. cell types), then go back and count how many vials I have with that content. It would be pretty easy to accomplish this as two separate statements, but that would involve iteration and two separate SQL queries. Best to leave it as one query where I count and select unique fields simultaneously:
Rails:
@summary = Ln2Location.select("count(*) count, contents").joins(:cryovial).where(:present => true).group(:contents)
SQL:
SELECT count(*) count, contents FROM `ln2_locations` INNER JOIN `cryovials` ON `cryovials`.`id` = `ln2_locations`.`cryovial_id` WHERE (`ln2_locations`.`present` = 1) GROUP BY contents
I’m selecting on Ln2Locations because there’s a property, “present,” which I’m using to select only the vials which haven’t been physically removed from the collection. Additionally, the relationship between the models is as follows:
class Cryovial < ActiveRecord::Base # Relationships belongs_to :user has_many :dewars, :through => :ln2_location has_many :ln2_locations accepts_nested_attributes_for :ln2_locations
class Dewar < ActiveRecord::Base # Relationships belongs_to :laboratory belongs_to :location has_many :cryovials, :through < :ln2_locations
class Ln2Location < ActiveRecord::Base # Relationships belongs_to :dewar belongs_to :cryovial
So only the child model, Ln2Locations, contains the foreign key that would be used to generate the join table.
Ruby on Rails: Integrating Devise
It is possible to integrate devise into your rails app as part of your user model in order to provide authentication support.
- add desired attributes to migration file
- make added attributes accessible in model file
- either roll your own views or add fields to generated views
That’s it. It really isn’t difficult or different than making any other model.
I was worried that it would be complicated, and spent a lot of time reading about devise to be sure. It’s so easy, no one writes about it.
Database Optimization: Indexing
After installing devise, I was looking through the migration file, and noticed a couple add_index commands. Reading up on what this actually does, I came across a couple interesting articles about database optimization:
Optimizing your MySQL Application
details: http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M001468&name=add_index
I don’t plan to build a giant Rails application, but Rails is known to have scalability issues. Still good to be aware of these points.
Ruby on Rails: on shared hosting, user authentication
* As usual, this post on Ruby on Rails is more documentation of my development process to supplement my notebook and memory. I can try to answer questions, but please keep in mind that I’m learning too.
I’ve bought the eBook version of the 4th edition of the Pragmatic Programmers’ Agile Web Development with Rails. It’s still in beta, but my main goal is to see how things have changed between Rails 2.x and Rails 3. The book also covers some basic user authentication, but it’s very simplistic. In the interest of getting my project up and going quickly, I looked into authlogic and devise/warden.
Both authlogic and devise/warden were highly recommended by mischa after building an application that did some direct comparisons of several different RoR user authentication solutions. Good enough for me. devise appears to be more lightweight, so I’ll try that one first.
A little bit about my server setup: I’m hosted with Lithium Hosting, which is a great deal and has fantastic customer service. They’re not paying me to pitch the service, though clicking the link above and then signing up for a plan gives me referral credits or something. It’s a shared hosting service, so I manage my site with cPanel if I need/want to, and I’ve added on SSH access to my account so I don’t really need to use cPanel.
Once the server upgraded to Rails 3.0/Ruby 1.8.7, a couple things happened: my rails apps stopped starting automatically (cPanel feature), cPanel could no longer start my rails apps, and I couldn’t create a new Rails app with a MySQL database.
Solutions:
1) install the mysql2 gem, which is the new mysql database adapter used by Rails 3. The compilers are disabled on my host, so this was done via support ticket.
2) add mongrel to my Gemfile,
#project/Gemfile
gem 'mongrel'
Mongrel isn’t automatically started in development mode. cPanel still won’t start my application, so starting the server is done via SSH with
rails server --port=[port]
So now I can create a new project
rails new project --database=mysql
and start the server. However, I don’t have a good way of authenticating users.
So, back to the support tickets to have both authlogic and devise installed. Once tech support has done that (seven minutes. really.), check if they’re installed for me:
gem server --port=[port]
and navigate to mysite.com:[port]. They’re not, run a couple commands from the command line:
gem install devise
gem install authlogic
Which should install the gems and make them available to me. Checking again as above, and both devise and authlogic are present. Add the appropriate lines to my Gemfile. Then, since I’m trying out devise first, install it:
rails generate devise:install
That’s all for now.
Ruby on Rails: Migrating data, too
Sometimes it makes sense to re-organize information in such a way that changing the database structure is necessary. It’s not terribly difficult to do this in Ruby on Rails.
I’m writing this more to file this away somewhere I won’t accidentally delete it:
Read the rest of this entry »
Ruby on Rails IDE
I’ve been trying to get back into web development as a hobby; there are many reasons for this, but maybe the most relevant at the moment is that it has the potential to make my actual job easier. If I can develop a website as an online tool, maybe best described as a content management system on steroids and tailored to my needs in the lab, I can spend more time planning and performing experiments, and less time actually running the lab by doing it more efficiently.
Recently, I’ve been using puTTY, WinSCP, and a text editor to build this project in Ruby on Rails. The problem is, I need a lot of real estate on my screen to be able to do this effectively, and I wasn’t able to get WinSCP to open all of my script files in the same text editor window. Combine this with the fact that I had to “touch” every file from the command prompt using puTTY after making changes in the text editor, and saving the modified file to the site with WinSCP — it was really just the least efficient process for designing and building a system to make my life more efficient.
Anyway, a couple years ago, I had stumbled upon RadRails, which was then rolled into the Aptana IDE. Looked good. It had syntax highlighting, which was a big plus. However, SFTP support for remotely working on a RoR project was, at the time, only supported in the paid, “Pro” version of the Aptana Studio suite.
Fast forward to this afternoon, when I re-discovered the Aptana Studio and RadRails, and then found out that the “Pro” version had been scrapped, and now SFTP is available for free!
I’m playing around with it now, but I’m happy so far.
web design, and differences in browser rendering
Admittedly, it has been a while since I last really truly developed a website.
The FaCU site wasn’t so much of a site as it was a page, and really, I feel I could have done a better job if I weren’t so ridiculously busy Senior year, and if I were more up to speed on using CSS to lay out websites.
Every time I’ve done this, beginning seven years ago, I’ve hit this block around how to manage the layout of a site. The old way, working with browsers that didn’t correctly render CSS styling code, a lot of people dealt with the problem by using <table> hacks, using these tags to generate a website layout composed of “invisible boxes”. All fine and good, except that it made changing even the smallest design element on the site was a huge undertaking, because every box was made to align and fit together like intricate pieces of a puzzle, and adjusting the thickness of one border or the geometry of one cell within the table, even by one pixel, required tweaking of every element around it, sometimes even going back and re-generating images or other graphics.
My first effort at using CSS to lay out a website was in high school, when it became my project to design and build a website for the high school newspaper. I used it to learn not only PHP and how to interact with a MySQL database, but really everything that would be involved in developing a website. It never became as full-featured as I wanted it to be, and it was eventually wiped out in favor of something less complex, and then again more recently for a WordPress-backed system.
Long story short, I tried to learn CSS in high school, found it supported by the various browsers available at the time in a very inconsistent manner (between browsers), and ditched it because it was too confusing to figure out how do exactly what I wanted with it.
I came back to CSS during my Senior year at Columbia while I was working on not only the FaCU website, but also the website for my Senior Design Project. I tried to adhere to only the most widely available fonts, and the lowest common resolution for both sites. The result: fairly ugly sites, but they get the job done.
Fast forward to today, when I’m now working on moving all of the lab’s most commonly used inventory books into a user-friendly digital form. CSS has better support across every browser, and everything just makes more sense now. No idea why.
Still, CSS is still rendered with quite a few annoying quirks. Take, for instance, the following page layout:

Lab inventory site
Renders just fine in Chrome, Firefox, and IE8. The left navigation panel and the main content to the left are spaced and positioned properly. In IE7, the main content title renders where it should, but the table appears at the bottom, under the navigation block. In Opera, the positioning offset I used to place the content area to the right of the navigation pane doesn’t appear to be necessary, so there’s a giant gap where there shouln’t be one.
A better situation than I was working with seven years ago, but still irritating.
Alternate WordPress posting options
As part of an effort to actually update this blog regularly instead o letting it slowly die like my old blogspot blog, I’ve been looking at a numer of different way of submitting new posts to this blog.
E-mail is nice, but typically I’m only at a computer either at home or at work… The problem is, only one of those places is an appropriate environment to be writing blog post, at least for me.
I do, however, spend a lot of time commuting, even after having moved closer to work. This presents a pretty good environment for writing blog posts, provided I don’t get motion sickness too quickly. The only issue is, I would need to find some way of efficiently writing blog posts and saving them to be uploaded once I regained acces to an Internet connection.
Surprise, surprise, there’s an iPhone/iPod Touch app that does exactly this, and it’s free! I’m using it now just to test it out, an it’s actually not too bad, though this might be more of a critique of extended typing on the iPod Touch than it is of the WordPress application.
Postie!
The built-in WordPress PHP script to grab e-mails and generate blog posts from them wasn’t particularly good, so I’m now using Postie (http://wordpress.org/extend/plugins/postie/), which seems to at least offer more features, though this post is really an experiment to see how well it works.
I keep having issues with the built-in WordPress installer, so I think I’m just going to use the command line and grab everything via wget.
Edit: This worked perfectly, and I definitely like Postie more than the built-in wp-mail.php method. Definite plus: being able to add tags to the post by e-mail, among other things. I’m going to have fun with this. Also, Postie supports POP3 with SSL without my ssl://server hack. I think it ends up working the same way, but the additional layer of abstraction makes it dead simple for a first-time user.


