Matt RaibleMatt Raible is a writer with a passion for software. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.
You searched this site for "form". 326 entries found.

You can also try this same search on Google.

Struts 1.2.8 has client-side validation issues just like 1.2.7

This weekend, I upgraded AppFuse from Struts 1.2.4 to 1.2.8. After failing to upgrade to 1.2.7, I was a little leary of this release - for good reason. It turns out, the 1.2.8 release has the same client-side validation issue as 1.2.7. The good news is it's a Commons Validator issue this time, and you can fix it by upgrading to Commons Validator 1.2.0 (it ships with 1.1.4).

If you're using custom client-side validators, you might have to patch your functions. Here's what I did to mine:

- oTwoFields = eval('new ' + formName.value + '_twofields()');
+ oTwoFields = eval('new ' + retrieveFormName(form) + '_twofields()');

Also, if you'd like to use Spring MVC with Commons Validator 1.2.0, you'll need to patch springmodules-validator. Or you can just download the one from AppFuse's CVS.

Posted in Java at Dec 19 2005, 04:55:37 PM MST 2 Comments

Remotely debug your app in Eclipse

Levent Gurses has a nice blog entry on Remote Debugging with Eclipse. I was able to use his instructions a couple months ago to attach to a remote instance of WebLogic (8.1 SP4) to debug an application. The beauty of his instructions is I had it all working in about 5 minutes. Yesterday I realized you could use the settings in *any* application server, and remotely debug it - or at least you should be able to. Today I noticed he added instructions for Tomcat and JBoss. Good stuff - thanks Levent!

Here's the settings you need in your startup script - just in case Levent's blog is down the next time I need this info.

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

Posted in Java at Dec 02 2005, 11:04:19 AM MST 20 Comments

Are you doing client-side validation in your JSF applications?

Are you doing client-side validation in your JSF application? I am in Equinox and AppFuse, but it seems somewhat primitive. I'm using David Geary's corejsf-validator.jar from his Core JSF book. It hooks into Commons Validator, and while it does work - it has some issues. First of all, if you have it in your classpath when testing, it screws up Ant-style path matching in Spring (applicationContext*.xml doesn't work). Secondly, it puts all the JavaScript validation functions right in your page, rather than in an external file that can be cached by your browser.

In reality, I believe that JSF should include client-side validation as part of the spec. After all, all of the other popular Java web frameworks support it. Then again, Rails is growing in popularity and AFAIK, it doesn't support it. In addition, the popular Java webapps I've seen (JIRA, Confluence, Jive Forums) don't use it either. I'm not trying to advocate that we should all be using client-side validation in our apps, I'm merely saying it should be an option in the standard web framework for Java.

The only public (and open source) option I've found is the Commons Validator support in Shale. Will this work in a MyFaces application w/o Shale? Are there other client-validation frameworks for JSF I don't know about? Or is the best option "don't use client-side validation with JSF?"

Posted in Java at Nov 24 2005, 09:02:39 AM MST 6 Comments

Lots of Java activity in San Francisco

The last 24 hours here in San Francisco have been quite interesting. Yesterday, I had lunch with a group of AppFuse users. They work for a company a few blocks from my training class. Chipotle was on the way, so I grabbed a burrito on my route and had a great time talking with them about the various open source tools that AppFuse uses, as well as what's on the roadmap. Thanks for the cookies John!

After class yesterday, I had a Guinness with an AppFuse user that recently put a high-volume site into production. He said it's held up surprisingly well and AppFuse greatly simplified his ability to deliver the project on time. In fact, most of the features the client wanted were already built-in.

Last night was another hotbed for Java talk - from web frameworks to TSSS in Vegas. Matt Filios and I had dinner and drinks with Mike "wanna play poker" Cannon-Brookes, Crazy Bob, Patrick Linskey, Geoff Hendrey (I hope I got the name right) and a number of other guys whose names escape me. It was Mike's birthday, so I left early to avoid the chaos that Crazy Bob and Mike always seem to stir up.

To top it all off, this morning I ran into a couple of folks that read this blog. I was getting breakfast at a local bagel shop - when a guy came up to me and asked "Is your name Matt"? I answered yes, and we talked briefly about my trip out here. It was kinda wierd being recognized, but kinda cool at the same time. It was good to meet you Nadeem.

I'm heading home from this wonderfully warm place tonight, but I'm sure I'll be back in the near future.

Posted in Java at Nov 18 2005, 12:00:57 PM MST 5 Comments

Made it to Java in Action

I just arrived at Disney's fancy "Yacht Club" for the Java in Action show. Today was a fun day - Julie and I took Abbie and Jack to Magic Kingdom and had a great time. Abbie got to meet Mickey Mouse, Winnie the Pooh, Eor and Tigger. She was scared of Mickey, but warmed up to Pooh and friends pretty quickly. It was weird - it kept raining off and on throughout the day, but it didn't seem to put a damper on anyone's spirits. Unlike Colorado, rain doesn't cool anything down. In fact, the humidity seems to crank the temperature up a notch or two.

Tomorrow is a full day - I have a 3 hour tutorial on web frameworks in the morning, followed by an hour of Ajax + Spring in the afternoon. After that, it's back to vacation-mode until I return to Denver next Tuesday.

I'd post the slides from my talks, but they're starting to make less and less sense (in downloadable form) as I add more images and less bullets. Besides, I plan on coding and conversing for most of the talks. That's the fun part of speaking at conferences - who wants to listen to a presentation anyway? Why does the good conversation have to take place in the hallways? Can't it happen right in the room?

Posted in Java at Oct 06 2005, 04:15:17 PM MDT 5 Comments

New Laptop

When I saw Russell's Why I Might Switch Back... post a couple of weeks ago, I found myself wanting to write a response. My response was going to be I completely agree and I was going to bitch about how slow my PowerBook is (once again). Then, later that day, I was doing something with iPhoto and I thought - I really do like OS X. It's the Mac hardware that I don't like. And it's not the look of the hardware (I love that), it's the fricken speed!! Most PowerBook users I know don't switch b/w computers a whole lot - whereas I spend 50% of my time on a fast Windows desktop. When I go from something that's so fast to something so slow, it's quite painful.

Last week, I started working with a new client - developing an application with Spring, Hibernate, WebLogic and Eclipse. Installing WebLogic on OS X was pretty easy, thanks to this article. Even remote debugging with Eclipse was pretty easy to setup. However, when I started running WebLogic locally and trying to debug it with Eclipse, it was extremely frustrating. I've never seen the spinning beach ball so much in one day. When other developers would watch me work, it was embarrassing how slow my computer was. And it's not like I had a whole lot running: Mail, Safari, Eclipse, WebLogic and iTerm.

Over the past couple of months, I've started debating if my next laptop should be a PC. It's not like I hate the Mac or don't like my PowerBook - but Java development on a Mac is far slower than on a PC equivalent. The problem is that I really like the PowerBook's form-factor. I'm so comfortable using the keyboard, right-clicking with the Control key, and all that jazz - that I'd probably have a hard time adjusting. I realize that a lot of my PowerBook bitching might seem unfair - as I'm often comparing a Desktop to a Laptop.

What I'd really like is two laptops: a PowerBook for doing all non-Java stuff and a PC for doing Java stuff.

My dreams came last Friday when my client handed me a brand new Dell Latitude D610. It's got Windows 2000, a 1.6 GHz CPU and 1 GB of RAM. To be honest, I expected it'd have a lot bigger processor. However, the fact that it doesn't makes it easier for me to show you how fricken slow my PowerBook is.

I used AppFuse for this test and ran ant clean war 3 times on each. I had ANT_OPTS set to -Xmx256m, JAVA_OPTS set to -Xmx512m and I'm using the latest 1.4.2 JDK available for each respective platform. It's possible my PowerBook suffers from some OS Rot, but it's still amazing how much faster the Windows laptop is.

  • PowerBook: 58.3 seconds
  • Latitude: 17.3 seconds

Holy ass-kicking batman!

My PowerBook has a 1.33 GHz CPU and 1 GB of RAM. It'd be interesting to do the see the numbers for a PowerBook with a 1.67 GHz processor. Here's to hoping OS X with a 1.6 GHz Intel processor can keep up with Windows for Java development.

Posted in Java at Oct 02 2005, 11:25:17 AM MDT 25 Comments

Open Source CMS Evaluation - Part II: Customization

This is the third and final post in my quest to find the best open source CMS for my needs. Previous posts include Building a website with an Open Source CMS and Open Source CMS Evaluation - Part I: Installation. Based on these two posts, reader feedback, and my installation experience, the final round of candidates include Joomla, Drupal, Magnolia, OpenCms and MeshCMS. These are listed in the order that I expected the final rankings to be - just to let you know what my feelings were going into this final process. ;-)

The CMS I choose will be used to build Virtuas's website, as well as customers of Virtuas. By using a CMS to produce websites, the design is separated from the content - and the "adding content" process can be much easier for the customer. This greatly simplifies the "creating a website" process for us, and will likely save our customers a fair amount of design costs. In addition, it makes it much easier for site owner's to maintain the site after it's been published.

As for Virtuas's site, it's static right now, and we'd like to change that. We want to ability to show recent blog posts on the front page, as well as make it easy for Practice Leaders to publish articles. In addition, it should be easy for our designers to change the design (1-2 files) and for our marketing team to add press releases and update existing content. Tomorrow, I'll be presenting my choice to the rest of the team, and we hope to design and start publishing content this week. Our goal is to have a new Virtuas site up and running one week from today.

My goal today was to see how easy each CMS was to customize. In addition, I wanted to see how easy it was to publish an article, as well as to aggregate our latest RSS feed titles onto the homepage. To test the design customizability, I tried to reproduce the current Virtuas homepage. Then I published Jeff's Geronimo Article, and attempted to aggregate feeds from Maria's and Bruce's blogs. My main reason for putting the Java CMS'es lower than the PHP ones in my suspected order of finishing is because I don't they don't have the RSS Aggregation feature.

Rather than just jumping in and using each CMS in anger, I tried to start off by reading the documentation for each. My main focus was on how to customize, but I also looked for an RSS Aggregation feature and ease-of-publishing for articles. I read documentation for 15-20 minutes, then dived into creating a custom theme and adding content. I installed each CMS on my PowerBook, and used Safari and Firefox on OS X, as well as Firefox on Windows XP in some cases.

MeshCMS - I spent 40 minutes looking into MeshCMS before I knew it wasn't the one. The main problem I had with it was the upgrade process. To upgrade to a new version, they recommend that you use symlinks to your files and store them in a separate location on the file system. While this may work for some, it seems a little brittle to me. I'd rather use a solution that keeps everything stored outside of the application by default. Creating a new theme was quite easy - but there didn't seem to be any support for multiple menus (i.e. global and local navigation), nor was there any means to customize the menu template.

I did manage to blow up the whole application at one point, simply because I was missing a JSP tag in my template. Since I had this template selected for the administration as well - it hosed the whole application and spit out stack traces for each page. Luckily, renaming the template directory caused MeshCMS change to revert to the default settings and everything was fixed. The interesting thing about MeshCMS is it looks very similar to the SiteMesh+JSPWiki CMS I wrote a few weeks ago. However, mine allowed full menu creation by editing/creating wiki pages.

OpenCms - When I installed OpenCms a couple of days ago, I initially did it on Windows. Everything worked fine and after waiting 18 minutes for everything to import, I was able to browse and edit the default site. However, today was a different story. The version I installed on my Windows XP box no longer works. When I got to http://localhost:8080/opencms/, I get a directory listing with "resources" and "setup" on it. The same day I installed OpenCms on Windows, I installed it on my PowerBook. It took 38 minutes to complete, but nevertheless, it said the process worked. Today I re-ran the setup and now I have the same result as on my Windows XP box. If the setup and installation is this fragile, I'm not interested. Blame me and the fact that I'm a redneck all you like, but the Magnolia installation is still functioning just fine. ;-)

Magnolia - I spent about a half hour with Magnolia before I knew it wasn't for me. While the admin UI is impressive with all it's Ajax goodies, creating a new template is cumbersome and not designer-friendly at all. You have to create a "new node" and then a bunch of "data nodes" under that. The documentation (a QuickStart PDF) is 17 pages long and forgets to mention the "title" data node is needed before the template will show up properly as an option. Once you've created a new template in the admin UI, you have to create the template on your file system - inside the web application. This may make it difficult to upgrade if you're deploying Magnolia as a WAR. The worst part is after creating the template, you have to restart the server. WTF? That seems a bit ridiculous to me. Granted you'll likely be designing your master template in a development environment - but good luck installing Magnolia for a client and having them create a new template.

One of the most interesting things about Magnolia is most of the folks who've recommended it have highlighted that it's "built on the revolutionary Java Content Repository Standard JSR-170". While I can admire the technical merits of this effort, it doesn't necessarily make this a good product. A good product, IMHO, is easy and intuitive to use. The admin interface for Magnolia is not intuitive. I like the fact that I can right-click on a page/node/etc., but on my Mac (with Firefox and Safari), the real context menu shows up on top of the application menu after a second or two. I'm going to pass on Magnolia due to the fact that its not designer friendly, as well as the fact that templates can't be edited in the browser. It looks like something that might be very interesting for developers, but it's simply not friendly for HTML developers.

At this point, it's 10:30 p.m. on Sunday night. I need to make a decision before I go to bed tonight and I'm scheduled to meet with our designer at 7:00 a.m. to start implementing his new design. I haven't started the PHP options, and I've had a couple new ones recommended on my blog while doing this evaluation today. Mal recommended Exponent and Jacob recommended MySource Matrix.

Because I'm down to two choices (and I haven't tried to customize either one), I decided it was worth looking at both of these PHP solutions. Exponent installed easy enough, but MySource Matrix failed miserably. Joomla, Drupal and Exponent all had an easy-to-use web installer that *just worked*. MySource spit out a bunch of permissions errors (even after chmod -R 777 *) and told me I had to run .php files from the command line. Since the other options all installed easily, I decided not to continue evaluating MySource Matrix.

Exponent - I didn't spend very long looking at Exponent. At first, I didn't think it had any documentation b/c it was a bit difficult to find on their site. Maybe it's because they don't have a background set on their site and my browser defaults to gray - making the gray text difficult to read. Even w/o documentation, I was able to navigate around the default Exponent site and figure out how to edit content. It's an easy UI to use, but again I was disappointed to find the "corporate" theme doesn't have a white background. Most good web designers know to set a default background color - and it always annoys me when someone misses this step. It's possible they don't set the background on purpose - like Yahoo does.

The deal-breaker with this CMS was that I couldn't edit any files w/in my browser to change anything (all I wanted was a white background). While it's theme management and templating looks powerful - it's another file-based system where you have to configure everything and then upload it. I agree that this is likely the path that web designers will want to use to get started - but I think it's important that files can be tweaked on-the-fly. Using a good FTP tool is certainly an option, but I'd prefer theme-editing to be part of my CMS. The one thing I did like about this CMS was the clean URLs. Granted, they aren't static-looking by any means, but having a simple ?section=# seems cleaner than the multiple parameters that other systems use.

Drupal - This CMS seems to have a lot of things I want/need as first class citizens. A blog, news feed aggregation and the ability to provide pretty URLs (aliases) for more cryptic CMS-type URLs. I couldn't get the URL aliases to work, but I suspect I was doing something wrong and didn't give it enough attention. I didn't spend a whole lot of time with this CMS, but rather just browsed around the admin interface and read a bit about how to create themes. I installed the PHPTemplate engine, but never installed any themes. When I found out I couldn't edit any of the uploaded templates contents, I started to get discouraged by Drupal. One thing I found disappointing, with both Drupal and Joomla, is they seemed to hard-code my server name into many of their URLs. When I installed these applications on my PowerBook, I used "localhost" for the server name. When testing out things from my Windows box, I couldn't even login to Drupal b/c it kept redirecting me to "localhost". Joomla had a similar problem with localhost, except that it only screwed up stylesheet paths. I was still able to administer the application.

Joomla - My time with Drupal was short-lived, mainly because I was itching to start playing with Joomla - which I've heard a lot of good things about in the past week. Furthermore, it's got a really good-looking administration UI. It's the type of UI that a designer would look at and appreciate. When trying to edit pages from my Windows box - everything worked, but I couldn't save the page. No JavaScript errors or anything, there was simply no reaction. Editing pages and content from my PowerBook solved the problem. I was able to easily create a simple theme that looked like virtuas.com and upload it. The theme isn't perfect, but it was easy enough to create using the Velvet theme from Joomlashack.com as a template. The weird thing about Joomla, at least with the default install, is there's no notion of pages. Everything is some sort of news item. In the pages I created, I was also unable to remove all the authoring notation and other junk that I don't want to show. The admin UI had options to remove the stuff, but even after "applying" the changes, they still showed up in the reader view.

It's now 1:30 a.m. on Monday morning and the last three CMS's definitely didn't get the attention they deserved. Nevertheless, I think they're the best of the bunch. Not only were they much easier (and quicker) to install than the Java options, but their UIs are also good-looking and easy to use. Drupal and Joomla both seem like excellent choices. Drupal seems to be more of what I'm looking for since it has all the features I want, and allows aliasing of URLs to make it appear like a static site. However, Joomla is a lot more eye-catching and that alone makes me want to use it. Neither of these CMS'es seem to have a full-featured blogging engine, at least not one that's as good as Roller.

Conclusion: I'm going to recommend we use Joomla, and look into doing some URL Rewriting to pretty up it's URLs. I doubt there's a whole lot we can do, but I'd like to figure out a way to make them a bit more search engine friendly. Drupal seems like an excellent choice as well, but the fact that I can't edit templates from the UI kinda sucks.

Thanks for listening y'all - all your comments and feedback during this evaluation have been great.

Posted in Open Source at Sep 26 2005, 01:32:58 AM MDT 27 Comments

Failed upgrade to Struts 1.2.7

Yesterday I made an attempt to upgrade AppFuse to use Struts 1.2.7 instead of 1.2.4. Everything seemed to go smoothly until I started running my Canoo WebTests with JavaScript enabled. It turns out there's a major bug (IMO) in Struts 1.2.7 where the JavaScript rendered by Commons Validator can't handle the Struts' <form> tag in XHTML mode. I tried a nightly build (20050809), but it's not fixed there either, so I backed out the upgrade.

The interesting thing about Struts 1.2.7 is SpikeSource has certified this release. Are they not testing JavaScript as part of their certification test?

Another thing I attempted to upgrade yesterday was Canoo WebTest from build574 to build976. No dice - mainly because of issues with Prototype (I think). The worst part is everything works fine in a browser. Even worse is I still have a problem with build574, but it doesn't cause the tests to fail. Times like this make me wish Selenium was more polished and ready to use. I talked with some ThoughtWorkers last week and they said the Java driver is ready for a 1.0 release, but one of the committers wanted to make sure all language drivers were ready first (at least that's what I remember).

Posted in Java at Aug 10 2005, 11:17:35 AM MDT 9 Comments

[OSCON] Spring MVC vs. WebWork Smackdown

Matthew Porter and I's Spring MVC vs. WebWork Smackdown presentation was a lot of fun this morning. We had a boxing bell (that I got off eBay) and had a good time ragging on the two frameworks. The only surprise was that Matthew actually ran some metrics on the Spring MVC vs. WebWork code in AppFuse and pointed out that the WebWork version required 25% less code than the Spring version. Oh well. The hard part about this presentation for me was trying to defend Spring MVC and saying it's better than WebWork. Matthew obviously felt strongly that WebWork was the better framework, whereas I like them both.

Posted in Java at Aug 03 2005, 05:15:43 PM MDT 7 Comments

[OSCON] Monday Afternoon

Ruby on Rails - Enjoying the ride of programming
Presented by David Heinemeier Hansson, OSCON 2005

About David: started doing Ruby in June 2003. Involuntary programmer of need, served 5 years in PHP. Spent 7 months in a Java shop.

Prerequisites of play: Ruby 1.8.2, dated December 25th. A database, pick one of 6. The RubyGems miner. Some gems called Active and Action.

Directory structure that Rails creates is more for convenience than anything. By picking conventions for you, it makes things easier. It might feel like flexibility is being ripped away from you - but you can change the defaults. However, by following the default settings, things will just work and life will be much easier for you as a developer.

I did a bit of playing on my PowerBook while listening to David's talk. I have Tiger installed, but found that Ruby 1.8.1 was installed on my machine (in /sw/bin/) thanks to Fink. My running "rm -r /sw/bin/ruby" and restarting iTerm, the default changed to /usr/bin/ruby, which is 1.8.2. From there, I downloaded and installed the Rails Installer.

I hate to admit it, but this talk is pretty boring so far. Probably because I've read David's blog for the past 6 months and watched most of the Rails videos. I haven't really learned a whole lot in the first 45 minutes of this talk. To be fair, the content of the talk seems to be properly targeted - there's been a fair amount of questions and everyone seems to be interested. Almost all of the seats are filled in the room; 3-4x as many folks as Dave Thomas's Ruby talk.

One interesting thing I've learned today is many features of Rails (i.e. Webrick) are actually a part of Ruby, not Rails. In addition, Ruby seems to have frequent releases and more features are added to the language each time. I guess that's the advantage of having a language that's not developed by committee.

When creating model objects in Rails, the default is to use a plural form of the object for the database table name. For example, a comment model will map to a comments table. Dave Thomas did mention in this morning's session that Rails isn't smart enough to figure out "sheep" - it gets maps to "sheeps". Apparently, you can easily override this behavior by specifying use_plurals=false somewhere. Another convention built-in to the framework is that the primary key is named "id" and its an auto-incrementing field.

"The database is a data bucket. I don't want any logic in my database, I want it all to be in my data model."

Rails doesn't handle composite primary keys. Rails is mostly designed for green-field development, where you get to control your database and its schema.

There are a number of key properties you can use in your database tables (a.k.a. your model objects) that will automatically get updated if you name them properly. Their names are created_at (datetime), created_on (date), updated_at and updated_on. There are also a number of time-related helpers, i.e. distance_of_time_in_words_to_now(date) » less than a minute ago.

Rails also has the concept of filters, which you can apply to a group of controllers. To use a filter, you define the filter method in controllers/application.rb and then you have to add a before_filter clause in each controller you want it to be applied. While it's cool that Rails has filters, it would be nice if you didn't have to configure the controllers that filters are applied to in the controller. To me, it seems more appropriate to be able to configure the where the filters are applied externally to the controllers. It seems more natural to me that you'd put something like apply_to_controller => { :controller1, :controller2 } in application.rb.

For doing page decoration with Rails (i.e. SiteMesh), you simply create a decorator in views/layouts. If you want a particular decorator to apply to a particular controller, you just name the file the same as the controller's URL. For example, if you have a posts controller (really a PostController.rb file), you'll create a decorator named posts.rhtml to decorate all the HTML rendered from the PostController - regardless of whether you're rendering from a method or from a view template. To have a decorator apply to all controllers, you can simply create a file named view/layouts/application.rhtml. This seems like something that SiteMesh could easily do as well - for example defaulting to /decorators/default.jsp (or something similar).

One thing I like about Rails is it's flash concept and how easy it makes it to display success messages. In my experience with Java web frameworks - many make this more difficult than it should be.

Testing Rails Applications

When running tests, Rails automates the creation of a test database instance that mirrors the schema of your development database. One slick thing in a Rails project's Rakefile is that you can run all the tests that you've touched in the last 10 minutes. I think one of the most unique thing about Rails/Ruby vs. Java is all that almost all of the files (Rakefile, code generation scripts, etc.) are written in Ruby.

Controller tests have a "mini-language" for simulating a browser when testing controllers. For example:

def test_login 
  get :login
  assert_response :success<
  assert_template "login" 
  
  post :login, :password => "secret!"
  assert_response :success
  assert !session[:authorized]
  
  post :login, :password => "secret"
  assert_response :redirect
  assert session[:authorized]
end

In the Controller tests, you can set cookies, parameters and mimic almost everything the browser can do. You can also test that your model objects have been manipulated appropriately. For example:

def test_create_post 
  post :create, :post => { :title => "This is my title", :body => "1" }
  assert_response :redirect
  assert_kind_of Post, Post.find_by_title("This is my title")
  
  post :create, :post => { :title => "", :body => "1" }
  assert_response :success # something was rendered, regardless of error messages
  assert_equal "don't leave me out", assigns(:post).errors.on(:title)
  #or assert_equal 1, assigns(:post).errors.count
end

The find_by_title method is a dynamic finder, where ActiveRecord creates find_by methods for each attribute of the model object. Another cool feature of testing is you can add a line with "breakpoint" in it - and the test will stop executing there - giving you access to all the variables at that point.

Ajax

The main reason for integrating JavaScript into Rails is so developers don't have to write JavaScript. For most developers, writing JavaScript is a pain because of browser incompatibilities and such. Rails ships with 4 JavaScript libraries, including Prototype and Script.aculo.us. It's easy to include the default JavaScript libraries in Rails:

<%= javascript_include_tag :defaults %> 

Both the link_to and form_tag methods have a "remote" equivalent (i.e. link_to_remote) that allows you to hook into Ajax, and by defining a :complete callback, you can call fade effects and the like. You can override many of the lifecycle stages of Ajax, but the most common is the :complete callback. In a Controller, it's easy to distinguish Ajax calls from non-Ajax calls using:

if request.xml_http_request?
  # do logic, for example rendering partials
end

Partials seem to be a pretty cool feature in Rails. They're actually just parts of a page that you include in a parent page with render :partial => "viewName". The slick thing about partials is you can actually populate their model and return them in a controller after an Ajax call.

The Ajax demos that David just showed are pretty cool. He was able to easily show how to delete a comment in his "weblog app", as well as add a new comment - w/o refreshing the page. The slick part of the add was he was easily able to add the new comment id to the Ajax response header, and then grab it in a callback and use the id to reference a <div> and use the yellow fade technique to highlight and fade the new comment.

That's the end of Dave's talk, and the first day at OSCON. Thanks to Dave and David for showing me the cool features of Ruby and Rails.

Posted in Open Source at Aug 01 2005, 05:02:31 PM MDT 5 Comments