Code that won't run

Basic role management

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

end
end

Alex Copot wrote:

Basic role management

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

params[:role_ids] is most likely a string.

$ irb
irb(main):001:0> ‘ab’.each { |s| puts “eacher: #{s}” }
eacher: ab
=> “ab”

How are you assembling the role_ids parameter on the webpage?

Stephan

<% form_remote_tag :url => { :action => ‘manage_roles’, :id => @user} do
%>
<% for role in @roles -%>
<% if session[:usersuper] %>
<input type=“checkbox” id=“role_id_<%= role.id%>”
name=“role_ids[]” value="<%= role.id %>" <%if @user.roles.include?
role%> checked=“checked”<%end%>/>
<% end %>
<%= role.name
%>

<% end %>

<%= submit_tag ‘Save’ %>
<% end -%>

What is the error you get?
Stephan

Yeap, I got it. The role_ids array was nul because none of the inputs
were checked and therefore it returned nil.

params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)} if
params[:role_ids] -> this seemed to help

Good.

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