help needed with where and how best to set a param

Once again, I'm writing about a pop-up window issue and need some help. Trying to insert a new record prior to the pop-up and other sorts of things has proven to be a much bigger problem so instead I'll have a default record that will serve as a placeholder for new pop-up window values.

In my main form, if I'm editing an order, the order id is sent to the pop-up window. Then my pop-up window uses that information. If it's a new order, the pop-up window doesn't have an order id.

To work around this, I'd like to set a default order ID that I'll use for new orders so that the pop-up window options work.

My link to the pop-up looks like this:

<%= link_to 'small window',{ :controller => 'codes', :action => 'pop_up', :id => @order}, :popup => ['codes', 'width=450,height=600,menubar=no,resizable=yes,scrollbars=yes'] %>

How can I override :id => @order so that I send in a default ID of 1?

If anyone has other, better design ideas, I'd love to hear them. I think I've exhausted all of the ones that I have seen.

Have you tried making this special route a :collection route instead of a :member route?

If it were a collection route then the :id would be optional... but it might also solve the bigger problem more gracefully. In your popup action you could have something like this:

@order = params[:id].nil? ? Order.new : Order.find_by_id(params[:id])

Then your view could build the url either for insert (new_record?) or update. If you're already taking advantage of the new Rails 'form_for @ivar' then it should be done for you.

HTH, AndyV

AndyV wrote:

Have you tried making this special route a :collection route instead of a :member route?

If it were a collection route then the :id would be optional... but it might also solve the bigger problem more gracefully. In your popup action you could have something like this:

@order = params[:id].nil? ? Order.new : Order.find_by_id(params[:id])

Then your view could build the url either for insert (new_record?) or update. If you're already taking advantage of the new Rails 'form_for @ivar' then it should be done for you.

I will definitely try the route. Right now I'm using the DRY Forms example in the new Advanced Rails Recipes book so I'd like to avoid converting the forms back, but I may need to do that anyway to denote required fields.

Thanks.

The DRY form recipe extends the FormBuilder so you'll get all the freebies that the FormBuilder is already providing -- like the ability to tell if you've got a new object to create or an existing object to update. If you follow the logic presented in the recipe you should be able to extend it to denote required fields, too. You should be able to provide a 'required' attribute in a similar way they provide the override for the label.

HTH, AndyV

AndyV wrote: If you follow the logic presented in the recipe you should be

able to extend it to denote required fields, too. You should be able to provide a 'required' attribute in a similar way they provide the override for the label.

I'm still learning my way around RoR. I see this in the DRY Forms helper code:

    @template.capture do       locals = {         :element => yield,         :label => label(field, options[:label])       }

Would I add something there to designate that I need the required fields to look a little different?

Here's the form field template and how it's displayed:   <span class="label">     <%= label %>   </span>

So if it's required, I want to add a red asterisk.

Thanks for helping a newbie.

Yes, you're in the right ballpark. There are a few different ways you could go.

First, I'd recommend that you piggyback on that 'option' hash. I'd probably use a value like 'required_field' or something like that. From that point you have the options. One way to approach it would be to use option[:required_field] to add a css class to the field.

    @template.capture do       locals = {         :element => yield,         :label => label(field, options[:label], :class=> options[:required_field] ? 'required' : 'label')       }

Another option would be to just add a '*' to the label. Right before the call to 'build_shell':

options[:label] ||= field.to_s.humanize # default value for label if none was provide options[:label] << " *" if options[:required_field] # Add asterisk if requred