tag:therailworld.com,2005:/posts theRailWorld.com blog 2011-04-04T19:04:58Z tag:therailworld.com,2005:Post/39 2011-04-04T19:04:58Z 2011-04-04T19:05:36Z green_light, Client Side Validation for Rails <p>Maybe it's me, but there seems to be a severe lack of good client-side validation plugins, which integrate with ActiveModel. So, my employer, <a class="rumblelabs.com">Rumble Labs</a> kindly gave me some time to write one of our own, for use with <a class="onotate.com">internal projects</a> and personal use.</p> <p><a class="https://github.com/overture8/green_light">green_light</a> is a client-side validation gem which makes use of the <a class="bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation Plugin</a>. The beauty of this gem, is that your validation rules are still defined in the model.</p> <p>Give it a go!</p> <p><a class="https://github.com/overture8/green_light">Download green_light</a></p> <p><a class="bassistance.de/jquery-plugins/jquery-plugin-validation/">Download jQuery Validation Plugin</a></p> Phil tag:therailworld.com,2005:Post/38 2011-02-17T20:35:23Z 2011-02-17T20:35:23Z VIM gnome-256color not known <p>This one always troubles me each time I need to re-install Ubuntu. The solution?</p> <pre><code> sudo apt-get install ncurses-term </code></pre> <p>Done.</p> Phil tag:therailworld.com,2005:Post/37 2010-09-22T12:58:39Z 2010-09-22T12:58:39Z Forms Disappeared when upgrading to Rails 3.0.0 <p>Just upgraded one of my apps from Rails 3.0.0rc4 to Rails 3.0.0. One issue - all the forms disappeared! After doing some hunting, I <a class="https://rails.lighthouseapp.com/projects/8994/tickets/5493-form_for-silently-failing-with-rails-300">found that</a> the issue was being caused by the HAML gem. To fix this, upgrade to the 3.0.18 release of the HAML gem.</p> Phil tag:therailworld.com,2005:Post/36 2010-07-15T10:23:39Z 2010-07-15T18:23:00Z Wildcard Subdomains with Dnsmasq <p><a class="therailworld.com/posts/30-Issue-with-localhost-subdomains-in-Rails-3">In a previous article</a>, I talked about using subdomains in Rails 3. Since then, <a class="bcardarella.com/post/716951242/custom-subdomains-in-rails-3">many</a> <a class="tbaggery.com/2010/03/04/smack-a-ho-st.html">people</a> have elaborated on the topic. There's even been a great <a class="railscasts.com/episodes/221-subdomains-in-rails-3">Railscast</a>.</p> <p>I've found Tim Pope's <a class="tbaggery.com/2010/03/04/smack-a-ho-st.html">post</a> particularly useful. Basically, he has set up a domain name that resolves to locahost/127.0.0.1. This save you from having to specify subdomains in you /etc/hosts file - which is a hassle!</p> <p>I'd like to offer another alternative using <a class="www.thekelleys.org.uk/dnsmasq/doc.html">Dnsmasq</a>. With Dnsmasq, you can set up a simple DNS server locally, then you can use an apache virtual host to deal with wildcard domains.</p> <h2>Install/Configure Dnsmasq</h2> <p>First, install Dnsmasq - I'm using Ubuntu (sorry for those who aren't).</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/35 2010-07-13T16:00:16Z 2010-07-13T16:00:16Z Interacting with Controllers via the app object <p>Sometimes it's useful to interact with your controllers via the console. This, as you may know, can be achieved with the <em>app</em> object. You can perform GET, POST, PUT and DELETE requests by doing the following:-</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/34 2010-07-05T20:39:41Z 2010-07-05T20:40:19Z ActionController:: InvalidAuthenticityToken with Destroy action <p>I was getting a ActionController::InvalidAuthenticityToken destroy error when using the Destroy method in my Rails 3 app. Turns out, when I was migrating to Rails 3, I forgot to include this in the html header:-</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/33 2010-07-01T10:38:21Z 2010-07-01T10:42:22Z fieldWithErrors is now field_with_errors in Rails3 <p>This one confused the hell out of me. Suddenly, my css styling of the <strong>fieldWithErrors</strong> div stopped working. Turns out this has been changed to <strong>field<em>with</em>errors</strong>. Check it out <a class="github.com/rails/rails/commit/72a3e4b77b0e6e249a534d41ae48acdedd2098cc">here</a>.</p> <p>The change seems to have taken place between beta 3 and beta 4.</p> Phil tag:therailworld.com,2005:Post/32 2010-06-22T09:24:23Z 2010-06-23T09:15:36Z Using vim with irb <p>Typing long-winded scripts into the irb prompt can be a pain in the ass! It's been annoying me for a while now, so I decided enough was enough.</p> <p>I found a way to run a ruby script (the one which is currently open in vim) through irb, then leave you at a irb prompt to execute further commands.</p> <p>Say you're editing a ruby script in vim...</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/31 2010-05-16T16:07:00Z 2010-05-16T16:07:42Z error_messages_for is_no_more <p>Worth noting that error<em>messages</em>for is not included, by default, in Rails 3. If you check your console server log you'll see this warning:-</p> <pre><code>DEPRECATION WARNING: error_messages_for was removed from Rails and is now available as a plugin. Please install it with `rails plugin install git://github.com/rails/dynamic_form.git`. </code></pre> <p>After you install the plugin, everything works as expected.</p> Phil tag:therailworld.com,2005:Post/30 2010-05-09T20:42:58Z 2010-07-06T12:17:41Z Issue with localhost subdomains in Rails 3 <p>While upgrading one of my Rails 2.3 apps to Rail 3, I came across a few issues when using subdomains with a localhost domain. I have a route like so:-</p> <pre><code> root :to =&gt; 'controller#page', :constraints =&gt; {:subdomain =&gt; "subdomain1"} </code></pre> <p>I had a test subdomain set-up in my /etc/hosts file as usual. For instance, here is the first line in my /etc/hosts file:-</p> <pre><code> 127.0.0.1 subdomain1.localhost </code></pre> <p>So, if I request the url subdomain1.localhost:3000, Rails 3 treats "subdomain1.localhost" as the <em>domain</em>. In other words, it is treating "localhost" as the TLD.</p> <p>To fix this I changed my test subdomain, in the /etc/hosts file, to have a TLD, like so:-</p> <pre><code> 127.0.0.1 subdomain1.localhost.local </code></pre> <p>And hey presto, as if by magic, it now works.</p> <p>UPDATE: Changed TLD to .local (makes more sense)</p> Phil tag:therailworld.com,2005:Post/29 2010-05-04T20:14:15Z 2010-05-04T20:19:43Z Compass CSS3 Cross-Browser Mixins <p>I've been having a great time using Compass when working on <a class="meetee.me">meetee</a> recently. In particular, I've been making use of the CSS3 mixins.</p> <p>The mixins for <a class="compass-style.org/docs/reference/compass/css3/text_shadow/">Text Shadow</a>, <a class="compass-style.org/docs/reference/compass/css3/box_shadow/">Box Shadow</a> and <a class="compass-style.org/docs/reference/compass/css3/gradient/">Gradient</a> have been really useful. No need to worry about any cross-browser compatibility issues - compass takes care of this for you. Plus you're left with more concise code.</p> <p>The <a class="compass-style.org/docs/reference/compass/">new documentation</a> is looking really nice as well! Highly recommended.</p> Phil tag:therailworld.com,2005:Post/28 2010-02-17T10:32:20Z 2010-02-17T10:32:20Z Using Rack Middleware in Rails3 <p>I was just looking into including some <a class="rack.rubyforge.org/">Rack</a> middleware in my Rails 3 app. One thing confused me slightly. There seems to be two ways that you can include middleware in you app:-</p> <h2>1. config.ru</h2> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/27 2010-02-12T20:44:12Z 2010-02-12T20:44:12Z Disable ActiveRecord in Rails 3 <p>Came up against this recently when trying to upgrade a Rails app which was using MongoDB. Obviously I didn't want to use ActiveRecord, so I set about removing it.</p> <p>The old way of removing ActiveRecord was to place this line in the environment.rb file, "config.frameworks -= [ :active_record ]". However, in Rails 3 you can simply pass the "-O" switch to the rails command, like so:-</p> <pre><code> rails yourapp -O </code></pre> <p>But what if you've already created your app? If you look in your config/application.rb file, you'll see this line at the top:-</p> <pre><code> require 'rails/all' </code></pre> <p>This does what it says on the tin; includes all rails modules. If you want to exclude ActiveRecord, remove the above line and replace it with:-</p> <pre><code> # Pick the frameworks you want: # require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" require "active_resource/railtie" require "rails/test_unit/railtie" </code></pre> <p>This is the beauty of Rails 3, pick and choose what suits you.</p> Phil tag:therailworld.com,2005:Post/26 2010-02-11T21:18:48Z 2010-03-18T13:58:22Z Using Prototype and JQuery with Rails3 (UJS) <p>I've been looking for some information on the unobtrusive javascript features in Rails 3, and it turns out that there's not much out there! Either that, or I'm looking in the wrong places.</p> <p>So here is my attempt to explain some of the features. I'll create two sample blog apps, one using Prototype and the other using JQuery (the code is available on github).</p> <h2>Install Rails 3</h2> <p>If you haven't already, get Rails 3 installed. Everything you need to know is <a class="guides.rails.info/3_0_release_notes.html">here</a>.</p> <h2>What is UJS?</h2> <p>In older versions of Rails there was a DSL called RJS. This basically allowed the user to write Javascript using Ruby. However, RJS was tightly coupled to the Prototype JS framework... so if you wanted to use JQuery or MooTools you would need to do make your own solution.</p> <p>This preference for Prototype lead people to want a more loosely coupled approach, which didn't discriminate against other JS frameworks. Enter UJS...</p> <h2>How it works</h2> <p>Rails 3 makes use of HTML5 data-* tags to store pertinent data along with the element in question. This removes the need for inline javascript to be stored in the element, as it was with delete links in the past. The main player in this is data-remote="true", which is used to tell the form or link that it should perform an asynchronous request to the server.</p> <h2>Javascript Drivers</h2> <p>The above is achieved with some help from a javascript driver file that uses Prototype by default but can easily be changed to a JQuery driver. The file is called rails.js and is located in the "javascripts" folder. Here are some of the drivers available:-</p> <ul> <li><a class="github.com/rails/prototype-ujs">Prototype</a></li> <li><a class="github.com/rails/jquery-ujs">JQuery</a></li> <li><a class="github.com/kevinvaldek/mootools-ujs">MooTools</a></li> </ul> <p>To use another driver simply swap the rails.js with the one you want to use and include the appropriate javascript library.</p> <h2>Let's give it a go!</h2> <p>I'm not going to take you through the whole process of creating the Rails app. What I will show you is the parts that relate to UJS requests.</p> <p>As I said this is a sample Blog app, so we have a Posts - Comments relationship set up. In the show.html.erb view we have a form for creating new comments:-</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/25 2010-01-20T11:55:23Z 2010-01-20T11:57:59Z Sorting an EmbeddedDocument with MongoMapper <p>I had this issue recently when trying to sort items in an EmbeddedDocument. Say you have a "Post" Document and a "Comment" EmbeddedDocument. If you want to sort the comments by the created_at field (which is pretty likely), it isn't as easy as you would think, using MongoMapper (at the moment anyway).</p> <p>However, there is a nice clean alternative that can be used to achieve the same results. Simply use the "sort_by" method like so, passing in a symbol of the field to sort as a proc:-</p> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/24 2009-11-26T21:02:57Z 2009-11-26T21:24:04Z A look at themes with Compass (Part 1) <p>As you may of heard, I'm working on a new project (<a class="meetee.me">meetee</a>), which will allow a more collaborative approach to meetings.</p> <p>Over the last few days I've concentrating my efforts on getting the website design implemented using the following technologies:-</p> <ul> <li>Ruby on Rails</li> <li>MongoDB</li> <li>Compass CSS Framework</li> </ul> <p>I've written about <a class="i.github.com/chriseppstein/compass">Compass</a> before, and in this article (and the next) I want to concentrate on how I dealt with setting up <strong>themes</strong>.</p> <p>Compass wouldn't be possible without <a class="sass-lang.com/">Sass</a>, the underlying language. Sass allows you to define variables (among other things), and this becomes particularly useful when defining base colours for your website theme(s). For instance, I defined the following base colours for my "Roast" theme. <em>In the below example, see how you can perform operations on hex colours to make a lighter or darker shade. This is important! When you change the base colours, the correct shades will be applied to the derived colours.</em></p> <h2>The Roast Theme</h2> <script src="/img/spacer.gif"> Phil tag:therailworld.com,2005:Post/23 2009-10-24T16:46:03Z 2009-10-24T17:09:53Z Sunspot Full-text Search for Rails/Ruby <h2>Sunspot</h2> <p>I had the recent pleasure of using <a class="outoftime.github.com/sunspot/">Sunspot</a>, which is powered by <a class="lucene.apache.org/solr/">Apache Solr</a>, to provide full-text search functionality for a Ruby on Rails recent project.</p> <h2>Installation/Configuration</h2> <p>Installing Sunspot is reasonably simple. I installed the Sunspot gems along with the related <a class="github.com/outoftime/sunspot_rails">Sunspot Rails Plugin</a>, with the following commands:-</p> <p><strong>Sunspot gems</strong></p> <p><code>sudo gem install outoftime-sunspot outoftime-sunspot_rails --source=gems.github.com</code></p> <p><strong>Sunspot Rails Plugin</strong></p> <p><code>script/plugin install git://github.com/outoftime/sunspot_rails.git</code></p> <p>Note that you will need to <code>require</code> the relevant library in the <code>Rakefile</code> which is in the base directory in your Rails app:-</p> <p><code>require 'sunspot/rails/tasks'</code></p> <p>Now you can generate the sunspot yaml configuration file with the built in generator:-</p> <p><code>script/generate sunspot</code></p> <p>This creates a <code>sunspot.yml</code> file in the rails <code>config</code> directory (more on this later).</p> <h2>Indexing Fields in your Models</h2> <p>Sunspot, in comparison to the alternatives, has really clean and clear syntax for defining indexed fields in <strong>the model</strong>. Here's an example:-</p> <script src="/img/spacer.gif"> Phil McClure tag:therailworld.com,2005:Post/22 2009-10-14T19:27:11Z 2009-10-14T20:42:35Z Sass and the Compass Framework <h2>My Experience with Compass</h2> <p>So, as you may have noticed, I have given the site a bit of a make over... and I thought it may be nice to make my first post be about the tools that I used to create the new look. In particular I want to talk about the wonderful <a class="compass-style.org/">Compass CSS framework</a>.</p> <h2>Compass</h2> <p>Compass is what I would call a "real" CSS framework. Tools like Blueprint and 960 Grid have been classed as frameworks but really, they are just grid systems. If we think of other types of frameworks like <a class="en.wikipedia.org/wiki/.NET_Framework">.NET</a>, <a class="cakephp.org/">Cake</a>, <a class="rubyonrails.org/">Rails</a>, they all provide an abstraction of the given language in order to save us from having to re-implement common functionality. With the help of <a class="sass-lang.com/">Sass</a>, this is exactly what Compass does.</p> <h2>Sass</h2> <p>It's worth saying a few word about Sass. Sass is an intermediate language which allows you to do a lot more than you can with standard CSS, the best of which are:-</p> <ul> <li>You can define variables</li> <li>You can define reusable blocks of code called mixins</li> </ul> <p>In addition to this the syntax is much more concise. With these great features Sass looks like it might outshine it's sister project <a class="haml-lang.com/">haml</a>.</p> <h2>Back to my experience with Compass</h2> <p>I used Compass along with Rails to create this site and it really has been the most pleasurable experience I've had when styling a site.</p> <p>When dealing with styling on past sites, I frequently ended up with one CSS file which included all the styling for the entire site. This is not a good way to approach styling for a few reasons. First of all, it makes it harder to create reusable pieces of code. Second, having all the code packed into one file results in unreadable unstructured code.</p> <p>Compass and Sass changes all of this and more! Compass provides some common functionality via mixins plus I can create reusable blocks of code using Sass. Further to this, Compass includes wrappers for grid sytems, such as Blueprint, so you can leverage off them. It doesn't end here - <strike>Compass</strike> <strong>Sass</strong> allows you to define "partials" similar to that of Rails. These are great for separating your code to make things more modular... and if you're thinking that these extra files will result in more http requests to the server, you'd be wrong. As I said, Sass is an intermediate language meaning it ultimately needs to be compiled into CSS - so you can potentially end up with one CSS file and you can even compile it in compressed mode!</p> <p>I can't recommend this approach enough. So, for you next project, give it a go.</p> <h2>Installing Sass</h2> <p><a class="sass-lang.com/download.html">sass-lang.com/download.html</a></p> <h2>Installing Compass</h2> <p><a class="wiki.github.com/chriseppstein/compass/getting-started">wiki.github.com/chriseppstein/compass/getting-started</a></p> Phil McClure tag:therailworld.com,2005:Post/20 2009-09-01T00:00:00Z 2009-10-02T22:57:33Z Caching with Rails Part 1: Page Caching <p><img src="/img/spacer.gif"> Phil McClure tag:therailworld.com,2005:Post/21 2009-09-01T00:00:00Z 2009-10-02T23:14:01Z Nested Object Forms in Rails 2.3 <h2>The Problem</h2> <p>If you work with web applications on a a regular basis, at some point you're going to have to deal with gathering user input for storage in a database or for passing on via email. As you start to model more complex "problem domains, you will find that the forms that you require for gathering related input become much more complex.</p> <p>For instance, let's take a simplistic example. A "Company" has many "Addresses" as shown by the diagram below.</p> <p><img src="/img/spacer.gif"> Phil McClure tag:therailworld.com,2005:Post/18 2009-08-01T00:00:00Z 2009-10-15T12:48:05Z Single Table Inheritance with Rails <h2>What is it?</h2> <p>With single table inheritance you have a base model which inherits from ActiveRecord::Base, then one or more sub-classes, which inherit from the base model.</p> <p>Single table inheritance is a software pattern described by Martin Fowler. Since (most) databases don't support inheritance, there is an issue when trying to map objects to database tables. This is known as the Object-relational impedance mismatch.</p> <p>Rails uses the Single Table Inheritance pattern to solve this problem. The basic idea is that another field in the base database table is used to store the type of the object.</p> <h2>Why use it?</h2> <p>If you want to acurately model a domain then inheritance is going to be necessary at some point or another. So single table inheritance gives you this flexibility.</p> <h2>Be Careful!</h2> <p>Just be careful when using single table inheritance. Since all the data from all the sub-types is include in one table, you can end up with a lot of "null"s scattered throughout the table. These ultimately increase the size of the table and you could end up with a scaling problem on your hands.</p> <p>For instance, say you have an abstract object called "Employee" and several sub-classes called "FullTimeEmployee", "TempEmployee" and "StudentEmployee". This is shown in the class diagram below:</p> <h2>Employee Hierarchy</h2> <p>As you can see, the super-class (Employee) has two instance variables "Name" and "Salary". Further to this, each of the sub-types have an instance variable relating, specifically to them. These instance variables in the sub-classes are what cause the problems. If you have a look at the table below, all will become clear:</p> <p>Type Name Salary Hours Duration University FullTimeEmployee Jim 10,000 37 null null TempEmployee John 15,000 null 5 null StudentEmployee Joe 20,000 null null Queens University, Belfast</p> <p>As you can see, each type has nulls in the fields which don't apply to it. This problem is only compounded when extra objects are added. So, as I already mentioned, the scaling problems can be serious, so keep this in mind when modeling your application.</p> <h2>Coding it</h2> <p>Using the "Employee" model above we first need to generate the Employee model:</p> <pre><code>ruby script/generate model employee name:string salary:string hours:string duration:string university:string type:string </code></pre> <p>Notice we have included a "type" field. This is used to store the type of object that the record applies to. Now migrate this into the database:</p> <pre><code>rake db:migrate </code></pre> <p>Now, in the app/models folder create a model file for each sub-class:</p> <pre><code>full_time_employee.rb </code></pre> <script src="gist.github.com/200186.js"></script> <p>We can now test this out using the console:</p> <pre><code>&gt;&gt; FullTimeEmployee.create!(:name =&gt; "Jim", :salary =&gt; "10,000", :hours =&gt; "37") &gt;&gt; TempEmployee.create!(:name =&gt; "John", :salary =&gt; "15,000", :duration =&gt; "5") &gt;&gt; StudentEmployee.create!(:name =&gt; "Joe", :salary =&gt; "20,000", :university =&gt; "Queens University, Belfast") </code></pre> <p>Now, print the records, we just created, to screen:</p> <pre><code>&gt;&gt; y Employee.all </code></pre> <p>!ruby/object:FullTimeEmployee attributes:</p> <pre><code>name: Jim updated_at: 2009-06-12 19:16:42 university: salary: "10,000" type: FullTimeEmployee id: "1" hours: "37" duration: created_at: 2009-06-12 19:16:42 </code></pre> <p> attributes_cache: {}</p> <p>!ruby/object:TempEmployee attributes:</p> <pre><code>name: John updated_at: 2009-06-12 19:19:50 university: salary: "15,000" type: TempEmployee id: "2" hours: duration: "5" created_at: 2009-06-12 19:19:50 </code></pre> <p> attributes_cache: {}</p> <p>!ruby/object:StudentEmployee attributes:</p> <pre><code>name: Joe updated_at: 2009-06-12 19:19:59 university: Queens University, Belfast salary: "20,000" type: StudentEmployee id: "3" hours: duration: created_at: 2009-06-12 19:19:59 </code></pre> <p> attributes_cache: {}</p> <p>See how the type field has been updated for you?</p> <p>Now, when we retrieve the employees, we want them to be of the appropriate type. Let's see if this works by getting all employees and calling the "class" m
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.