teesea
February 26, 2007, 10:02am
1
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
teesea
February 26, 2007, 7:37pm
2
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
teesea
March 1, 2007, 2:50am
3
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
teesea
March 1, 2007, 4:09am
4
Nevermind My category_id was a string…oops.
On Feb 28, 8:49 pm, “[email protected] ” [email protected]
teesea
February 26, 2007, 11:58pm
5
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. [email protected]