Cancel Forms And Avoid The If Then Statements

October 16th, 2008

One of the big pains with Rails form helpers is that they don't have a cancel button. No, problem - that is easy enough to add in. Just use a second submit tag like so:

            <%= f.submit "Create" %>
            <%= submit_tag "Cancel" %>

Now, if the user clicks the "Cancel" button a "commit" param with a value of "Cancel" will be submitted.

This is nothing earth-shattering. I have found this technique on various blogs. But then they all suggest checking for this parameter in the update or create method of your controller. I don't like this because it makes you add in extra "if-then" statements. I really hate "if-then" statements. They make the code so much harder to read.

So what to do?

Use a before filter.

before_filter :check_for_cancel, :only => [:create, :update]

Then add in your method to check for the cancel parameter in the commit message.

  private
  
  def check_for_cancel
    if params[:commit] == "Cancel"
      redirect_to admin_spaces_path
    end
  end

That's it. So now, if anyone clicks on the "Cancel" button on an edit or new form they will be redirected to the index.

If you really wanted to you could make the Cancel button submit the cancel parameter as part of the model hash. You would do it like this:

            <%= submit_tag "Cancel", :name => "model[cancel]" %>

You would just need to update your check_for_cancel method to check for that param. I prefer the previous method though since it is more generic.

ScreenSteps Live: A Case Study on Ruby on Rails - Part 2 - Figuring Out the Code

October 14th, 2008

In my last post about developing ScreenSteps Live with Ruby on Rails (which was far too long ago) I talked a lot about what led to me getting started with Rails development. In this post I want to talk about what were some of the things about Ruby and Rails that I found really useful and what things were really confusing.

Once again - a little of my background. When I started this I had a music degree from the Berklee College of Music, knew some basic HTML and was at least competent at using CSS thanks to the book "CSS: The Missing Manual", which I can't recommend enough.

So I was by no means a programming expert. Concepts such Classes, Modules, ActiveRecord, View Helpers, Partials, Arrays, and Hashes were all totally foreign to me. I had done some database work several years ago where I got to dig into SQL commands a bit. This background at least gave me a good understanding of how relational databases worked.

The Stuff That Confused Me

Here are some of the things that initially confused me about Ruby and Rails:

Block notation

It looks something like this:

@posts.each {|p| p.some_method}

For some reason that totally confused me. Once you figure it out it is wonderful and very powerful. But as I read books and searched the internet I could find all these things that mentioned blocks but didn't define what they were or how they worked. I think that I will do a post soon about the important things I wish I had known about blocks but I don't have space for that here. Now that I understand them I can find plenty of information on them. Isn't that always the case?

Ternary operators

url.blank? ? "Preview not available" : url

That type of stuff totally threw me for a loop. All it is doing is evaluating an expression and returning a value depending on the value returned by the expression. It is short hand for an if-then statement. But those ? and : are very intimidating when your code vocabulary is limited.

Array Operators

posts.collect {|p| p.title } posts.select {|p| p.title == "My Post"}

I didn't like that stuff at all, mainly because I wasn't comfortable with arrays yet. That compounded with the block notation had me dong a lot of head scratching.

The Stuff I Liked

Read the rest of this entry »

Filtering Feeds from NetNewsWire For Your iPhone

July 11th, 2008

This will show you how to quickly customize the feeds you receive in NewNewsWire on your iPhone. I got the original instructions here.

Login to NewsGator

wpid94-media-1215789000263.png

Select Settings

wpid95-media-1215789108905.png

Select Edit Locations

wpid96-media-1215789121891.png

Select Feeds for iPhone

wpid97-media-1215789137453.png

Scroll down to the NetNewsWire iPhone/iPod-Touch section and select Feeds.

Uncheck Feeds

wpid98-media-1215789158528.png

Uncheck the feeds you don't want to see on your iPhone.

wpid99-media-1215789172111.png

Select Update.

ScreenSteps Live: A Case Study on Ruby on Rails - Part 1

May 21st, 2008

At Blue Mango Learning Systems we primarily develop two applications:

Distributing a product for Mac, PC and running a hosted web service is a lot of work, especially when there are only two people in your company. Therefore we have had to choose our development tools very carefully. For us, the most important aspect of any development tool is our ability to quickly iterate over a product until we feel that we have it "right". Really, for us, all other considerations fall way behind. If we can't iterate efficiently then we are going to move on to another tool.

Ruby on Rails has proven to be a great tool for this type of development. There are a lot of people getting into Ruby on Rails so I thought some of you might be interested in my experience. I think that ScreenSteps Live is an interesting case study in the benefits and drawbacks of Rails, so over the next couple of months I will be posting some of the things that I have learned as I have dived into the Rails world.

First, a little background on myself as a programmer and ScreenSteps Live as a web app.

ScreenSteps Live Struggles to Come To Life

I have no background as a programmer. Well, almost none. We first got the idea for ScreenSteps Live back in late 2006 or early 2007. At that time I had never even attempted to program a web application. I knew some basic html, enough to throw up a web page, and enough php to do some basic includes. But nothing beyond that. I also had programmed some smaller desktop apps with Revolution but only after extensive help from Trevor.

But we needed a web app. Trevor wasn't going to have time to do it so we decided to outsource. I won't go into all of the details on this but our first attempt didn't work out so well. Suffice it to say that after 2 months we had nothing and were out a few thousand dollars. All I can say is be very meticulous in checking out the people you are hiring to develop your stuff. We weren't and it came back to bite us in the form of lost time and lost money.

Read the rest of this entry »

A Little Revolution Does A Developer Good

May 20th, 2008

Round here we develop all of our desktop applications using Revolution. I recently attended the RunRevLive 08 conference in Las Vegas where one of the speakers was Robert Cailliau, the co-developer of the web. Robert spoke on “Programming systems and the birth of the Web”. Following a very interesting presentation on how the web came about he discussed some characteristics he thought a good programming language should have:

  • You should not need the reference manual lying open next to you.
  • A good assignment operator.
  • The less "swear" characters the better. These are characters from the top of the keyboard and are like the top shelf at a newsstand - dirty.
  • You should not need lots of comments.

His point was that you spend most of your time reading your code, not writing it. Therefore the language should be easy to read and understand. Since Revolution uses an english-like syntax it fulfills all of Robert’s criteria.

Read the rest of this entry »