Select tag not defaulting to Models value

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

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!

I just ran in to the same problem, I added the "to_s" to the end of c.id and that fixed the problem

<p><label for="question_category">Category</label><br/> <%= 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

Nevermind :slight_smile: My category_id was a string...oops.