# Dynamic select box

**URL:** <https://www.ruby-forum.com/t/dynamic-select-box/160579>\
**Category:** Rails\
**Created:** [February 17, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/dynamic-select-box/160579 "2009-02-17T23:52:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 17, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/dynamic-select-box/160579/1 "2009-02-17T23:52:47Z")

</div>

Hi everyone,

I have a problem. I want to display a different select box wether the  
current user is an admin or an user.

So I thought I could do this in my view where my form is displayed :

\<%= f.label(:role\_id, 'Role:') %\> \<% if (current\_user.admin?) then collection\_select(:user, :role\_id, Role.find(:all, :order =\> "name desc"), :id, :name) else # Here I would code an another collection\_select end %\>

But It doesn’t work. Nothing is displayed!  
Could you help me?

Thanks!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 18, 2009, 12:08am UTC](https://www.ruby-forum.com/t/dynamic-select-box/160579/2 "2009-02-18T00:08:13Z")

</div>

On Feb 17, 2009, at 5:52 PM, Guillaume L. wrote:

> \<%  
> \<%=  
> ^  
> if you want output, you generally need \<%= (if you get to the point  
> that you know to generate output from \<% %\>, then you will KNOW when  
> to use them)

-Rob

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 18, 2009, 12:32am UTC](https://www.ruby-forum.com/t/dynamic-select-box/160579/3 "2009-02-18T00:32:29Z")

</div>

Rob B. wrote:

> On Feb 17, 2009, at 5:52 PM, Guillaume L. wrote:
> 
> > \<%  
> > \<%=  
> > ^  
> > if you want output, you generally need \<%= (if you get to the point  
> > that you know to generate output from \<% %\>, then you will KNOW when  
> > to use them)
> 
> -Rob

I did this :

\<% if(current\_user.admin?) then %\>  
\<%= collection\_select(:user, :role\_id, Role.find(:all, :order =\> “name  
desc”), :id, :name) %\>  
\<% end %\>

And now it displays something. Thx!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 6:00am UTC](https://www.ruby-forum.com/t/dynamic-select-box/160579/4 "2009-02-19T06:00:15Z")

</div>

I would do 2 things to try to fix your problem if I were you:

1. Make sure you know the type of user you have when you get the  
request for the page
2. Put the logic of generating the data in the controller, then use an  
instance variable to display the values and forget the ‘if then else’  
logic in the view. Your view will be much easier to read and modify.

My guess is that the problem you have is that you don’t have a  
current\_user type value (or not the one you believe you have) by the  
time your code runs.

Pepe

On Feb 17, 5:52 pm, Guillaume L. \<rails-mailing-l…@andreas-
