[Rails] Rails 101 - Validation

Stefan Arentz stefan.arentz at norad.org
Fri Dec 10 09:49:53 GMT 2004


On Dec 10, 2004, at 10:20 AM, Jarkko Laine wrote:

> Stefan,
>
> On 10.12.2004, at 10:08, Stefan Arentz wrote:
>>
>> There are two problems with this code:
>>
>> When I call /countries/add, the code form field is initialized to 0.  
>> I don't want to have a default value in there. Where does it come  
>> from?
>
> I guess text_field will by default set the value according to your  
> country object (see  
> http://api.rubyonrails.org/classes/ActionView/Helpers/ 
> FormHelper.html). You could probably set it to nil manually.
>
>>
>> Also, when I submit the form with the code form field set to '123a',  
>> the validation correctly kicks in and sets the error message. But it  
>> also resets the code field to '0'. The name field is displayed  
>> correctly however.
>
> I think the problem might be that when you render_action "add" after  
> the failed submission, your prior inputs are not relayed to that  
> action.

But they are, because the name field is setup again correctly after the  
POST. Also, my code was really:

   def add
      @country = Country.new(@params["country"])
   end

> Moreover, the function "add" will call Country.new again, making the  
> code to be zero. Don't know why the name works, tho.

See above, wrong paste in the email :)

>
> I've used this (ugly) hack to get the fields correctly prepopulated  
> after a failed form submission. I use the same action in both cases:

Too ugly, too much work. The framework should do this correctly.

  S.



More information about the Rails mailing list