A A

Using Javascript to Populate Forms During Development

Sun, Dec 7, 2008

Automation, Code, Ruby on Rails, Snippets

During development, working with forms quickly gets annoying because you have to constantly fill in each field, sometimes with unique info. One way around this is to write a little Javascript code that just populates the fields. I use something like this on the bottom of the form. I had jQuery no-conflict mode on in this case. In your app you might be able to get away replacing _j() with $():

<% if ENV['RAILS_ENV']=='development' -%>
<!-- Generate random field data -->
<script lang="text/javascript">
  jQuery(function() {
    if (typeof(_j)=='undefined') _j = jQuery;
    function randstr() {
      return Math.floor(Math.random()*99999)
    }
 
    _j('#name').val('Chuck Norris');
    _j('#company_name').val('Roundhouse LLC');
    _j('#email').val('moo'+randstr()+'@yawhoo.com');
    _j('#login').val('user'+randstr());
    _j('#password').val('admin');
    _j('#password_confirmation').val('admin');
  })
</script>
<% end -%>


Add me. I'm lonely Why not subscribe to the feed?. If you’re on a mobile device I suggest Viigo

Tags: , , ,

1 Comments For This Post

  1. Donny Kurnia Says:

    You could use this instead

    jQuery(function(_j) {
    // code using _j

    });

    So you don’t need the line:
    if (typeof(_j)==’undefined’) _j = jQuery;

Leave a Reply