# Code that won't run

**URL:** <https://www.ruby-forum.com/t/code-that-wont-run/83983>\
**Category:** Rails\
**Created:** [January 31, 2007, 7:09am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983 "2007-01-31T07:09:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:09am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/1 "2007-01-31T07:09:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:24am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/2 "2007-01-31T07:24:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:26am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/3 "2007-01-31T07:26:05Z")

</div>

\<% 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 -%\>

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:38am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/4 "2007-01-31T07:38:15Z")

</div>

What is the error you get?  
Stephan

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:32am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/5 "2007-01-31T07:32:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:41am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/6 "2007-01-31T07:41:13Z")

</div>

Good.

---

<div class="post-metadata">

**Author:** ![copot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@copot](https://www.ruby-forum.com/u/copot)\
**Post date:** [January 31, 2007, 7:43am UTC](https://www.ruby-forum.com/t/code-that-wont-run/83983/7 "2007-01-31T07:43:57Z")

</div>

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
