Sterling Rose Design Blog

Dynamic form with a many-to-many relationship

1 Comments
Tags: Javascript Rails associations

My customer’s application is an in-house ordering system (among other things). The order form has a drop-down list to select the client placing the order. It also has two other drop-down lists: primary_contact_id and secondary_contact_id, both of which are based on the Contact model. My customer is certain he will only ever need to associate two contacts with an order, so I’m comfortable with this approach.

The sticking point was that I only want to populate the primary and secondary contact select boxes with contacts that are associated with a particular client. Client and Contact have a many-to-many association.

Ryan Bates’ Dynamic Select Menus Railscast got me most of the way there, but his example (countries/states) was one-to-many, not many-to-many. Following was my adaptation:

views/javascripts/dynamic_contacts.js.erb

var contacts =...
Read the whole post...

Calculating Line Item Extensions

0 Comments
Tags: Javascript Rails Prototype AJAX

In my project, I have orders, and each order can have an unlimited number of line_items. Line_items are created by the user clicking on a button, which appends (via RJS) a new row to the line_items tabled form. So far, so good.

But I needed the extended price (quantity * price_per) of each line_item to be calculated every time the user tabbed out or clicked away from the price_per field. Further, I needed the subtotal, tax, total, and balance fields to be automatically re-calculated.

I messed around with it for several hours, trying Javascript, Prototype, and even jQuery, before I finally settled on a Prototype approach that worked. The real struggle was that using Rails 2.3’s nested forms functionality meant that each line_item would have an index key embedded in the middle of the text field’s name and id, and I could not come up with a good way to extract it, to pass it to the Javascript function.

Luckily...
Read the whole post...

assert_include Workaround

7 Comments
Tags: testing Rails

I’m not sure if there used to be an assert_include method or not (I’ve seen some references to it, but can’t find it anywhere in the Rails API), but I found a workaround.

I have a Color model, and I want the :index action to only show those colors whose deleted value is set to false (it’s more or less what the acts_as_paranoid plugin did/does). My test for the :index action looks like this:

test "should get only colors that are not deleted" do
  first_color = Color.create(:name => 'Blue', :deleted => false)
  second_color = Color.create(:name => 'Red', :deleted => true)
  get :index

  assert_response :success
  assert_not_nil assigns(:colors)
  assert_equal true, assigns(:colors).include?(first_color)
  assert_equal false, assigns(:colors).include?(second_color)
end


There may be other ways to do this (and I certainly wel...

Model Component Ordering

2 Comments
Tags: Rails community

On a whim, I asked (in IRC and via Twitter) what order – given validators, accessors, and associations – developers typically listed these components in their models. As far as I know there is no compelling reason to use one order over the other (but feel free to correct me in the comments). Even so, it can be fun to know what other developers do.

I had 19 responses, broken down as follows:

Accessors | Associations | Validators = 20%
Accessors | Validators | Associations = 5%
Associations | Accessors | Validators = 20%
Associations | Validators | Accessors = 45%
Validators | Accessors | Associations = 5%
Validators | Associ...

Perpetual Footer

2 Comments
Tags: date and time Rails

I was spinning up a brand new client website that would (like most websites) include a copyright footer. I wanted to make sure I wouldn’t have to come back year after year and update the year span, so here is what I used:

&copy; 2009<%= "-#{Date.today.year}" if Date.today.year != 2009 %> ABC Company


It will display only 2009 as the copyright year until January 1, 2010, then it will start displaying a span (from 2009 through the current year). Simple.

Uses Rails 2.3.2

Practical Tips for Attending a Rails Conference

0 Comments
Tags: community RailsConf Rails

I just finished attending my first-ever RailsConf in Las Vegas, NV. It was an amazing experience that I’ll be blogging about a lot over the next coming days. I wanted to make my first post on the subject be a (long – sorry) list of tips and advice in case you ever plan to attend a similar event yourself. If you have tips of your own that you’d like to share please feel free to drop them in the comments section!
  1. Bring lots of business cards. One of the greatest benefits of attending a conference of any type is the contacts you will make, and it’s easier for them to remember who you are and keep in touch if they have your contact information.

  2. Take care of your feet. There is a lot of walking and standing, and the days are quite long. Wear very comfortable, well-padded shoes. Put your feet up when you can. If you can find a place to unobtrusively take your shoes of...
Read the whole post...

RailsBridge: Bringing Awesome People Together to Do Awesome Things

0 Comments
Tags: Rails community RailsBridge education

Over the last couple of weeks, a motivated group of people took the See a need, fill a need approach to development in general, and the Rails development community in particular. The needs we saw were:

a) Make a concerted, focused effort to encourage a sort of “culture of kindness,”
b) Promote a “give back” mentality by developing applications in Rails for non-profit organizations,
c) Support greater inclusion of all kinds of minorities, and
d) Provide a wealth of documentation, mentorship, and instructional material to new developers.

With those objectives in mind, we have launched RailsBridge. Our guidelines are pretty simple:

* First, do no harm. Then, help where you can.
* Bridge the gap from aspiring developer to contributing community member, through mentoring, teaching, and writing.
Read the whole post...

Using AJAX to Change a User's Role

2 Comments
Tags: Rails AJAX

Scenario: Client wants to display a list of users, and the role each user currently has. The user’s role should be the selected option in a select box of all roles. Administrator should be able to update the user’s role by simply selecting a new role from the select box for that user, without having to reload the page.

Approach: Each row in the user’s list should have an id that uniquely identifies it as being associated with that particular user. Each select box should also have an id field associated with the user. Choosing a new role should trigger the onchange event, which executes the update on the role and displays the new user’s row in place, without reloading the entire user list.

Code:

## app/views/users/index.html.erb

 <table>
   <% @users.each do |u| %>
     <tr id='user_row_<%= "#{u.id}" %>'>
       <td><%= link_...
Read the whole post...

eWeek Covers Rails 2.3 RC 1

0 Comments
Tags: Rails media

Darryl K. Taft of eWeek asked several of us who tweet about Rails what we thought about yesterday’s release of Rails 2.3 Release Candidate 1. Here is the article, along with my own thoughts.

Rendering datetime in a user's timezone

0 Comments
Tags: Rails date and time

We recently had a client who wanted to account for a user’s time zone when they entered dates for various fields. For example, if a user were to input a new bill, the bill entry date should default to the current date. Time.now is what we had been using, but that would only give the right date if you were on the correct side of the magic UTC dividing line.

The solution to this problem involved 1) storing a user’s time zone in a string field in the User model, and 2) adjusting Time.now for that time zone.

For part 1, we used Rails’ built-in time_zone_select method:

<%= form.label :time_zone, "Time Zone" %>
<%= time_zone_select :time_zone, @user.time_zone, nil, :default => 
  "Pacific Time (US & Canada)" %>


For part 2, the solution was to add a method to the User model to get the user’s current date:
Read the whole post...


Copyright 2007-2010, Sterling Rose Design. All rights reserved.