# Help needed with where and how best to set a param

**URL:** <https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754>\
**Category:** Rails\
**Created:** [May 16, 2008, 12:16am UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754 "2008-05-16T00:16:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 16, 2008, 12:16am UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/1 "2008-05-16T00:16:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 16, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/2 "2008-05-16T20:42:33Z")

</div>

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

On May 15, 6:16 pm, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 16, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/3 "2008-05-16T22:11:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 17, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/4 "2008-05-17T22:28:39Z")

</div>

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

On May 16, 4:11 pm, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 21, 2008, 3:17am UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/5 "2008-05-21T03:17:25Z")

</div>

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

On May 20, 4:11 pm, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 20, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/6 "2008-05-20T22:11:20Z")

</div>

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:  
  
\<%= label %\>

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

Thanks for helping a newbie.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 27, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/7 "2008-05-27T18:09:10Z")

</div>

Becca G. wrote:

> Any thoughts on this one? Again, this works great when I’m editing a  
> record because I can save to the existing order, not so great with new  
> orders.

Just thought I’d bump this given that the US had a holiday weekend. Any  
thoughts? Please see previous post for details.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 23, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/help-needed-with-where-and-how-best-to-set-a-param/137754/8 "2008-05-23T23:33:02Z")

</div>

AndyV wrote:

> @order = params[:id].nil? ? Order.new : Order.find\_by\_id(params[:id])

I’m working on this with many other projects so I’m slow to getting the  
code in to test.

I implemented the above code, but here’s where the other gotcha comes in  
when I have a new order. Right now in my drag and drop, I’ve got a div  
that displays all of the selected items. When I drag items there now,  
it replaces whatever I have previously selected with the new dragged  
item. Now I know this is due to how I have it set up. I’ve got the  
partials and controllers hitting the database each time an item is  
selected. I save the value in the order and then call it back to show  
the selected items.

It seems to me that I’m going to need to create a persistent variable  
that will contain the string of values of the item id’s. But the other  
gotcha in this is that when an item is selected, it is removed from the  
list of available options. This makes it more user friendly so someone  
can see what they’ve chosen.

Any thoughts on this one? Again, this works great when I’m editing a  
record because I can save to the existing order, not so great with new  
orders.

THANKS!!!
