# Select tag not defaulting to Models value

**URL:** <https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834>\
**Category:** Rails\
**Created:** [February 26, 2007, 10:02am UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834 "2007-02-26T10:02:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [February 26, 2007, 10:02am UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834/1 "2007-02-26T10:02:06Z")

</div>

I know this is a basic problem with a very simple solution but I’m new  
to ruby and rails so I’m a bit confused why this isn’t working

I want to be able to create a select tag which is linked with a  
particular model, so I used this code in the view

\<%= start\_form\_tag :action =\> ‘index’%\>

```
<%= text_field("user", "user_name")%>
<%options = [["","0"], ["Low","1"],["High","2"]]%>
<%=select("user", "level", options)%>

<%= submit_tag 'test'%>

```

\<%= end\_form\_tag %\>

here’s the controller code

class TestController \< ApplicationController

def index  
@user = User.find\_by\_id(params[:id])  
end

end

I tested the above code using a user record with the field level  
containing 2 and the select list did not default to 2 but stayed at  
the empty option

As I said before, I’m sure I’m missing something obvious here but I  
haven’t a clue what, any ideas?

Cheers

Teesea

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [February 26, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834/2 "2007-02-26T19:37:43Z")

</div>

Add The following to the top of the file.

* * *

```
<%=params[:user].inspect -%>
```

* * *
Check to see what level is set to.

Frefesh the page to see changes to a form can be problimatic as well, as  
the browser will often remember what it was set to. (this drives me nuts  
when dealing with default values)

teesea wrote:

> I know this is a basic problem with a very simple solution but I’m new  
> to ruby and rails so I’m a bit confused why this isn’t working
> 
> I want to be able to create a select tag which is linked with a  
> particular model, so I used this code in the view
> 
> \<%= start\_form\_tag :action =\> ‘index’%\>
> 
> ```
> <%= text_field("user", "user_name")%>
> <%options = [["","0"], ["Low","1"],["High","2"]]%>
> <%=select("user", "level", options)%>
> 
> <%= submit_tag 'test'%>
> 
> ```
> 
> \<%= end\_form\_tag %\>
> 
> here’s the controller code
> 
> class TestController \< ApplicationController
> 
> def index  
> @user = User.find\_by\_id(params[:id])  
> end
> 
> end
> 
> I tested the above code using a user record with the field level  
> containing 2 and the select list did not default to 2 but stayed at  
> the empty option
> 
> As I said before, I’m sure I’m missing something obvious here but I  
> haven’t a clue what, any ideas?
> 
> Cheers
> 
> Teesea

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [March 1, 2007, 2:50am UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834/3 "2007-03-01T02:50:56Z")

</div>

I just ran in to the same problem, I added the “to\_s” to the end of  
c.id and that fixed the problem

Category  
\<%= select("question", "category\_id", Category.find(:all, :order =\> "description").collect {|c| [c.description, c.id.to\_s] }) %\>

I’m surprised rails doesn’t handle this for you when creating the  
select.

Jim

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [March 1, 2007, 4:09am UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834/4 "2007-03-01T04:09:09Z")

</div>

Nevermind 🙂 My category\_id was a string…oops.

On Feb 28, 8:49 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [February 26, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/select-tag-not-defaulting-to-models-value/86834/5 "2007-02-26T23:58:29Z")

</div>

Thanks for the tip, turns out the problem was that the field was of  
type int but the option values i was giving rails were strings, I  
didnt realise that it checked type when doing that, doh!

On Feb 26, 6:37 pm, David H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
