<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jim Neath &#187; Javascript</title>
	<atom:link href="http://jimneath.org/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimneath.org</link>
	<description>Ruby on Rails, Javascript, CSS and Standards</description>
	<lastBuildDate>Thu, 17 Jun 2010 12:37:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SWFUpload, Paperclip and Ruby on Rails</title>
		<link>http://jimneath.org/2008/05/15/swfupload-paperclip-and-ruby-on-rails/</link>
		<comments>http://jimneath.org/2008/05/15/swfupload-paperclip-and-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 15 May 2008 12:49:47 +0000</pubDate>
		<dc:creator>Jim Neath</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[SWFUpload]]></category>
		<category><![CDATA[Upload]]></category>

		<guid isPermaLink="false">http://jimneath.org/?p=23</guid>
		<description><![CDATA[Edit: Added paperclip validations to photo model. Fixed a bug with the js (thanks to Kevin). Commented the js file and added all methods used by SWFUPload.
SWFUpload
SWFUpload is a small JavaScript/Flash library to get the best of both worlds. It features the great upload capabilities of Flash and the accessibility and ease of HTML/CSS.
SWFUpload is [...]]]></description>
			<content:encoded><![CDATA[<p><em>Edit: Added paperclip validations to photo model. Fixed a bug with the js (thanks to <a href="http://www.mpet45.co.uk/">Kevin</a>). Commented the js file and added all methods used by SWFUPload.</em></p>
<h4><a href="http://www.swfupload.org">SWFUpload</a></h4>
<blockquote><p>SWFUpload is a small JavaScript/Flash library to get the best of both worlds. It features the great upload capabilities of Flash and the accessibility and ease of HTML/CSS.</p></blockquote>
<p><a href="http://www.swfupload.org">SWFUpload</a> is a brilliant tool built with flash and javascript that allows you to upload multiple files at the same time, allows you to display progress bars and various other useful things. The front end is left wide open by the SWFUpload API so it gives you complete control over how the UI works.</p>
<h4><a href="http://www.thoughtbot.com/projects/paperclip">Paperclip</a></h4>
<p>As many of you might have realised, I love <a href="http://www.thoughtbot.com/projects/paperclip">Paperclip</a>. It&#8217;s a great plugin that takes care of all the grunt work and lets you get on with other stuff. I&#8217;ve covered <a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/">how to use Paperclip</a> before.</p>
<h4>Making Rails Play Nice with SWFUpload</h4>
<p>There are a couple of pitfalls when using SWFUpload with your projects. I&#8217;ve covered them below:</p>
<h5>Scrambled Mime Types</h5>
<p>This is an annoying problem caused by the upload facility in Flash. It basically means that all the files you upload via SWFUpload have the content type of <code>application/octet-stream</code>, which in most cases isn&#8217;t what you want.</p>
<p>To fix this you will want to use the <a href="http://mime-types.rubyforge.org/">Mime-Types gem</a>. To Install, simply do the following:</p>
<pre><code class="ruby">gem install mime-types</code></pre>
<p>Now that you&#8217;ve got the mime types gem installed remember to require it somewhere in your code:</p>
<pre><code class="ruby">require 'mime/types'</code></pre>
<p>The way I&#8217;ve used it in the test project at the bottom is instead of assigning files to a model via <code>@photo.file = params[:photo][:file]</code>, I&#8217;ve created a custom method that fixes the mime type then passes the file to the correct method.</p>
<pre><code class="ruby"># Fix the mime types. Make sure to require the mime-types gem
def swfupload_file=(data)
  data.content_type = MIME::Types.type_for(data.original_filename).to_s
  self.file = data
end</code></pre>
<p>This takes care of the first problem.</p>
<h5>Problems with Sessions</h5>
<p>Flash has problems dealing with Rails sessions so a couple of quick fixes are needed. The first is to hack the sessions class. The fix can be found <a href="http://pastie.caboo.se/178902">here</a>.</p>
<p>I&#8217;ve included that code into a file called <code>swfupload_fix.rb</code> and placed it inside my initializers folder, so it&#8217;s loaded by the app is started.</p>
<p>Next add the following to your controller that handles the uploads:</p>
<pre><code class="ruby">session :cookie_only => false, :only => :create</code></pre>
<h5>The Controller</h5>
<p>By default, SWFUpload passes the post variable of <code>Filedata</code> when uploading. This can be changed easily if needed. The way I&#8217;ve dealt with the uploading in the test application is as follows:</p>
<pre><code class="ruby">def create
  # SWFUpload file
  if params[:Filedata]
    @photo = Photo.new(:swfupload_file => params[:Filedata])
    if @photo.save
      render :partial => 'photo', :object => @photo
    else
      render :text => "error"
    end
  else
    # Standard upload
    @photo = Photo.new params[:photo]
    if @photo.save
      flash[:notice] = 'Your photo has been uploaded!'
      redirect_to photos_path
    else
      render :action => :new
    end
  end
end</code></pre>
<p>There&#8217;s probably a better way to do it, but for an example it shows you to logics of everything.</p>
<h5>Linux and Flash Upload Progress</h5>
<p>As Tim mentions in the <a href="http://jimneath.org/2008/05/15/swfupload-paperclip-and-ruby-on-rails/#comment-202">comments</a> there is a problem with displaying upload progress in Flash for Linux. It is covered around the net and alos mentioned in the <a href="http://demo.swfupload.org/Documentation/#uploadProgress">documentation for SWFUpload</a>:</p>
<blockquote><p>The uploadProgress event is fired periodically by the Flash Control. This event is useful for providing UI updates on the page.</p>
<p><strong>Note:</strong> The Linux Flash Player fires a single uploadProgress event after the entire file has been uploaded. This is a bug in the Linux Flash Player that we cannot work around.</p></blockquote>
<p>So basically it&#8217;s boned on linux and there&#8217;s nothing you can do about it.</p>
<h4>SWFUpload and Paperclip App</h4>
<p>Hopefully, this should get you on your way to using SWFUpload with Paperclip in Rails. However, I&#8217;ve created a small app that should help you on your way to getting things working. If you have any problems or find any thing that could be done better, let me know and I&#8217;ll fix them up.</p>
<p>It has been tested on Firefox 2.0, Safari 3.1.1 and IE 7 on Windoze Vista. Not tested on IE 6 because, quite frankly, it can fuck right off.</p>
<p>To try the app: unzip the file, change to the swfupload directory then run the following:</p>
<pre><code>rake db:migrate
ruby script/server</code></pre>
<p>Then you should be able to see it by visiting <a href="http://localhost:3000">http://localhost:3000</a>.</p>
<div class="download">
<a href='http://jimneath.org/wp-content/uploads/2008/05/swfupload1.zip'>SWFUpload Test Project</a></p>
<p>A test project to show how to get Ruby on Rails, SWFUpload and Paperclip to play together nicely.</p>
</div>
<p>You can also grab the example application from github:
<pre><code>git://github.com/JimNeath/swfupload---paperclip-example-app.git</code></pre>
<h4>Credit Where Credits Due</h4>
<p>A lot of places helped be get my head around this stuff. So A massive thanks to Andy Stewart over at <a href="http://blog.airbladesoftware.com/">Air Blade Software</a> and <a href="http://www.digitaria.com/">Dylan Vaughn</a>.</p>
<h4>More Resources</h4>
<ul>
<li><a href="http://demo.swfupload.org/Documentation/">SWFUpload API Documentation</a></li>
<li><a href="http://code.google.com/p/activeupload/">ActiveUpload &#8211; A plugin that facilitates the use of SWFUpload</a></li>
<li><a href="http://www.vimeo.com/906589">SWFUpload + attachment_fu + restful_authentication + amazon s3 + rails 2 = hard, but doable.</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jimneath.org/2008/05/15/swfupload-paperclip-and-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>89</slash:comments>
		</item>
		<item>
		<title>More Usable Forms</title>
		<link>http://jimneath.org/2008/05/12/more-usable-forms/</link>
		<comments>http://jimneath.org/2008/05/12/more-usable-forms/#comments</comments>
		<pubDate>Mon, 12 May 2008 15:50:36 +0000</pubDate>
		<dc:creator>Jim Neath</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://jimneath.org/?p=24</guid>
		<description><![CDATA[I just thought I&#8217;d post about a few things that you should be doing with your forms and also a few things I just like to do.
Use Labels
I still don&#8217;t understand why there are a load of people who don&#8217;t use labels within their forms. They&#8217;re more semantic that using strong tags like alot of [...]]]></description>
			<content:encoded><![CDATA[<p>I just thought I&#8217;d post about a few things that you should be doing with your forms and also a few things I just like to do.</p>
<h4>Use Labels</h4>
<p>I still don&#8217;t understand why there are a load of people who don&#8217;t use labels within their forms. They&#8217;re more semantic that using <code>strong</code> tags like alot of people seem to do. They&#8217;re also more user friendly. Also they&#8217;re useful for styling your forms.</p>
<blockquote><p>The LABEL element may be used to attach information to controls. Each LABEL element is associated with exactly one form control.</p></blockquote>
<p>And here&#8217;s how you&#8217;d use it in your code:</p>
<pre><code class="html">&lt;label for="username"&gt;Username&lt;/label&gt;
&lt;input type="text" name="username" id="username" /&gt;</code></pre>
<p>Which gives you:</p>
<p><a href='http://jimneath.org/wp-content/uploads/2008/05/basic-label.png'><img src="http://jimneath.org/wp-content/uploads/2008/05/basic-label.png" alt="" title="basic-label" width="226" height="32" class="aligncenter size-full wp-image-27" /></a></p>
<p>Now, when a user clicks on the label it will focus on the form item specified by the <code>for</code> attribute. Hooray.</p>
<h4>User the Pointer Cursor</h4>
<p>Here&#8217;s the CSS:</p>
<pre><code class="css">label, button, input.button
{
    cursor: pointer;
}</code></pre>
<p>Easy. It does require you to add a class to submit and reset buttons, but hey it&#8217;s a small sacrifice.</p>
<h4>Focus on the First Field</h4>
<p>Having to click on the first form field I want to fill in is a pain in the ass. Don&#8217;t make me do it. Here&#8217;s a quick jQuery function to do it for you:</p>
<pre><code class="javascript">$(document).ready(function() {
    $('input[type=text]:first').focus();
});</code></pre>
<h4>No Confusing Buttons</h4>
<p>If you have a submit button and a reset button, or anything similiar, make sure people can tell the difference. I don&#8217;t want to fill in your huge form and then find I&#8217;ve accidentally clicked the reset button instead of the submit button.</p>
<p>An example is shown below:</p>
<p><a href='http://jimneath.org/wp-content/uploads/2008/05/buttons.png'><img src="http://jimneath.org/wp-content/uploads/2008/05/buttons.png" alt="" title="buttons" class="alignnone size-fullwp-image-29" /></a></p>
<p>Submit buttons on the left and sub actions on the right, see:</p>
<blockquote><p>Always put the Submit Form button on the left and on the Clear Form button on the right. Never, ever put the Submit Form button on the right and the Clear form button on the left.</p></blockquote>
<p>See?</p>
<p>I&#8217;ll post more when as they come to me, in my bizarre usability dreams. Feel free to post your own tips. Or to tell me that I know nothing at all about anything.</p>
<h4>More Reading</h4>
<ul>
<li><a href="http://www.keyrelevance.com/articles/usability-tips.htm">HighRankings Forum: Collected Web Site Usability Tips</a></li>
<li><a href="http://www.seoconsultants.com/html/forms/labels/">Usability Features &#8211; The Label Element</a></li>
<li><a href="http://webdesign.about.com/od/forms/a/aa050707.htm">What Makes a Web Form Usable or Unusable</a></li>
<li><a href="http://www.sitepoint.com/article/steps-useable-forms">7 Steps to Useable Forms</a></li>
<li><a href="http://www.alistapart.com/articles/sensibleforms">Sensible Forms: A Form Usability Checklist</a></li>
<li><a href="http://mattobee.com/blog/article/qa-checklist-online-forms/">QA Checklist: Form Usability</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jimneath.org/2008/05/12/more-usable-forms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
