Does anybody know if the options for a select in a form are accesible via
@params? I can''t even access which options are selected.
If someone is interested about the details, read on...
I have two tables: roles and permissions with a
"has_and_belongs_to_many"
relationship between each other.
In the edit action for role, I initialize role and permissions like this:
@role = Role.find(@params[:id])
@permissions = Permission.find (:all, :order => "title")
In the model I have two lists: one with all the available permissions and
another with the permissions that a specific role has and buttons that move
stuff from one list to the other using javascript.
The rhtml code for the lists goes like this:
<select id="available_permissions"
name="available_permissions" multiple
size="10">
<% available_permissions = @permissions - @role.permissions %>
<%= options_for_select (available_permissions.collect { |p| [p.title,
p.id]}) %>
</select>
<select id="role_permissions" name="role[permissions][]"
multiple =
"multiple" size="10">
<%= options_from_collection_for_select(@role.permissions, "id",
"title")
%>
</select>
I have two buttons which invoke a javascript method to remove an item from
one list and add it to the other list. It goes like this:
function moveSelected(orig, dest)
{
var selectOrig = document.getElementById(orig);
var selectDest = document.getElementById(dest);
var i = 0;
while (i<selectOrig.length)
{
option = selectOrig.options[i];
if (option.selected)
{
option.selected = false;
selectOrig.remove(i);
if (nn6)
{
selectDest.length = selectDest.length+1;
selectDest.options[selectDest.length-1] = option;
}
else selectDest.add(option);
}
else i++;
}
}
This part works well with records that I added manually to the
permissions_roles intermediate table. Nevertheless the simple
if @role.update_attributes(@params[:role]) ...
is not intelligent enough to populate role.permissions and update the tables
in the database. That is fine, as long as I can access the select options
from the controller and populate it manually, but whatever name I put in
params doesn''t seem to work.
The hash returned by @params[:role] or @params["role"]
doesn''t include
"permissions" (i.e. @params[:role]["permissions"] is nil).
Changing the name
to something else like id = "assocperms" name =
"assocperms[]" doesn''t help
as @params["assocperms"] also is nil (and so is
@params["assocperms[]"],
etc.). Also removing the "[]" from the name didn''t do the
trick.
Is my only alternative to pass the info in hidden fields?
If anyone can provide insight on how I can tackle this problem it will be
greatly appreciated!
Andres