views:

105

answers:

2

I have a simple form where I set the default value of a select list as follows

f.select :post,  (1..5).to_a, {:value => @author.posts }

if validation fails on the create action this list is always reset to 1.

In the create method after testing validation if I set

@author.posts = 5

then it will set the list to 5. However it does not set the list based on what was submitted.

Have tried to set

@author.posts = params[:post]

however it defaults to 1, using debugger have verified that params[:post] has the value selected (eg 5).

Is there some pointer stuff going on I'm missing? Any suggestions?

A: 

Does you development.log tell you anything? Maybe Rails isn't allowed to write the posts attribute to the model?

Edwin V.
A: 

You could be accidentally comparing strings to numbers. Form parameters come back as strings, and your 1..5.to_a is generating an array of integers. it's also possible that @author.posts is returning a string as well.

Try this:

f.select :post,  (1..5).to_a, {:value => @author.posts.to_s }

In any event, check your data types.

Also, check to ensure that your @author.posts value is indeed filled in.

Brian Hogan