def manage_roles
if params[:id] @user = User.find(params[:id]) @roles = Role.find(:all)
# if user submits the form then update
if request.post?
@user.roles.delete_all
params[:role_ids].each {|r| @user.roles <<
Role.find_by_id(r.to_i)}
# EXECUTION STOPS HERE
# IF I REMOVE THE LINE ABOBE, THE CODE WORKS
# if ajaxed -> do some visual thingy
if request.xml_http_request?
render :update do |page|
page.visual_effect :SlideUp, 'userroles'+params[:id]
end
else
# elseif regular request -> go back to the list of users
redirect_to(:action => 'list')
end
else
# if not submited just display the form
if request.xml_http_request?
render :update do |page|
page.replace_html 'userroles'+params[:id], :partial =>
‘manage_roles’
page.visual_effect :SlideDown, ‘userroles’+params[:id]
end
else
render :partial => ‘manage_roles’, :layout => ‘admin’
end
end
I didn’t get any error. The code simply stopped executing after that
line.
But I got it now.
When the user didn’t selected anything (role_ids is an array of values
returned from checkboxes), params[:role_ids] was nil and it stopped the
execution.
returns nil when params[:role_ids] is nil -> stopping the execution ->
the code after this line never executed
params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)}
#This was a problem only when the user didn’t check any of the
checkboxes. #If he checked an inputbox the code following that line worked.
#To prevent this from happening in this case (when none of the values
are selected), I simply added ‘if params[:role_ids]’ to ensure that this
line is only executed when the user selects something.
@user.roles.delete_all - #deletes all roles
params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)} if
params[:role_ids] # - only executed when at least one value is checked
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.