Topic: Using select box for site navigation

I would like be able to use a select box to link to my various reports, rather than listing all of them on the page.  Does anyone know how to implement this?  I tried...

timecards.rhtml

<select name=navi onChange="go()">
<option value=''>--- Select your report ---
<option value="/reports/current_work">Current Work
<option value="/reports/ready_workl">Ready Work
<option value="/reports/not_ready_work">Not Ready Work
<option value="/reports/complete_work">Complete Work
</select>

control.js

function go()
{
	box = document.forms[0].navi;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

But, I get a "document.form(0) has no properties error"

I'm also trying to do something similar, where I list a collection in a select box.  I'd like to be able to pick a person, and link to a different view.  I've tried...

employee_list.rhtml

<u>Show all</u><br>
		<select name="pl" id="pl">
		<option value="" selected>Choose</option>
			<% @employees.each do |p| -%>
			<option value="<%= p.id %>">
				<%= p.last_name + " , " +p.first_name %>  
			</option>
		<% end -%>
		</select>

<%= observe_field('pl', :frequency => 0.20,
   		                   :url => {:controller => 'timecards', :action => 'show_all_projects'},
				   :with => "'id='+value") %>

Does anyone know of a more Rails friendly way of accomplishing either of these two things?

Last edited by zreed20 (2006-07-20 10:00:46)

But they that wait upon the Lord shall renew their strength

Re: Using select box for site navigation

Hey zreed,

For the second, you could do something like the following:

<u>Show all</u><br>
<%= select("employee", "employee_id", Employee.find_all.collect {|e| [ e.last_name + ', ' + e.first_name, e.id ] }, {}, { :onchange => "window.location='/timecards/show_all_projects/'+this.value;" } ) %>

and for the first, do this:

<select name=navi onchange="if(this.value){window.location=this.value;}">
<option value=''>--- Select your report ---
<option value="/reports/current_work">Current Work
<option value="/reports/ready_workl">Ready Work
<option value="/reports/not_ready_work">Not Ready Work
<option value="/reports/complete_work">Complete Work
</select>

and you won't need any other javascript.

I hope those both work... it's late so I'm not quite sure if everything is correct big_smile

"Take up your cross before your crown" :: http://no-spec.com

Re: Using select box for site navigation

Thanks pnomolos! Why I didn't think to use a collection, even when I mentioned it in my first post I'll never know smile  This should work.  I'll try it this afternoon.

Zack

But they that wait upon the Lord shall renew their strength