Topic: Ruby/Rails code inside a Javascript string

Ok, I always have a problem with using JavaScript and Ruby/Rails.

So here's what I'm trying to do.  I have a select field that is giving a list of teams, and the <option value="?"> is the team id, something like this,

<%= select(:team, :id, team_selection_matrix, {}, :onchange => "display(this,'#{@team.id}');") %>

What I would like the "onChange" option to do is list those ids.

In my controller @team = Team.find(:all) so it will find them without a problem.

Thanks!

Re: Ruby/Rails code inside a Javascript string

the problem is, that will generate html like this:

onchange="display(this,'1 2 3 4 5 6 7 8')"

which is going to break your javascript..

So you probably need to do:

<%- ids_to_display = @team.id.join(",")
<%= select(:team, :id, team_selection_matrix, {}, :onchange => "display(this,[#{@team.id}]);") %>

This will generate:

onchange="display(this,[1,2,3,4,5,6,7,8])"

-patrick

Last edited by patrick99e99 (2008-09-01 22:21:10)