I''m trying to figure out what the standard pattern is for simple form management. Consider the following table: table country: int id int code varchar name Like: 1 45 Denmark 2 31 The Netherlands I have a controller to add a new one: class CountriesController def add @country = Country.new end def add_post @country = Country.new(@params["country"]) if @country.save then redirect_to(:action => "list") else render_action "add" end end end With a simple form: (the show error is a test of a helper that shows an optional error) <form action="add_post" method="post"> Code: <%= text_field("country", "code") %> <%= show_error(@country, "code") %><br> Name: <%= text_field("country", "name") %><br> <input type="submit" value="Add country"> </form> Ok all looks simple. But my problem is with validation. My Country model object looks like this: class Country < ActiveRecord::Base def validate errors.add("code", "invalid") unless code =~ /[0-9]*/; end end 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? 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''m not sure what I''m doing wrong here. S.
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. Moreover, the function "add" will call Country.new again, making the code to be zero. Don''t know why the name works, tho. 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: In controller: submission = @params.has_key?("trainingpart") @sel_discipline = submission ? @params["trainingpart"]["discipline_id"].to_i : nil Now, if this was a submission, I set some variables like @sel_discipline for the form. In training.rhtml: <%= options_from_collection_for_select(@disciplines, "id", "name", @sel_discipline) %> Now the selected discipline is preselected in case of a failed submission. I would like this to be more automatic, tho, and am not sure if there would already be a way to do it (but would sure like to know :). Now I have to do a lot of manual work to get the form fields prepopulated. //jarkko> > I''m not sure what I''m doing wrong here. > > S. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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.
On 10.12.2004, at 11:49, Stefan Arentz wrote:>> > > Too ugly, too much work. The framework should do this correctly. >Wholeheartedly agreed :) //jarkko> S. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> 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 believe this bug is fixed in Rails 0.9. I can''t find it in the changelog, but I just tested a similar situation in an app of mine and it works like you would expect it to. Beware, that if you set the default column value to 0, then it''ll initialize the field with a 0. (I really should get Rails 0.9, shouldn''t I :)?) ...also, render_action "add" just renders the template. It doesn''t call CountriesController#add. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
On Dec 10, 2004, at 11:54 AM, David Heinemeier Hansson wrote:>> 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 believe this bug is fixed in Rails 0.9. I can''t find it in the > changelog, but I just tested a similar situation in an app of mine and > it works like you would expect it to. Beware, that if you set the > default column value to 0, then it''ll initialize the field with a 0. > > (I really should get Rails 0.9, shouldn''t I :)?) > > ...also, render_action "add" just renders the template. It doesn''t > call CountriesController#add.I upgraded to the beta-gems but it still behaves the same. I feel pretty stupid not getting such a simple web app concept to work :) Does anyone have a working example of this? S.
Stefan, On 10.12.2004, at 23:41, Stefan Arentz wrote:> > On Dec 10, 2004, at 11:54 AM, David Heinemeier Hansson wrote: >> Beware, that if you set the default column value to 0, then it''ll >> initialize the field with a 0. >> >> (I really should get Rails 0.9, shouldn''t I :)?) >> >> ...also, render_action "add" just renders the template. It doesn''t >> call CountriesController#add. > > I upgraded to the beta-gems but it still behaves the same. I feel > pretty stupid not getting such a simple web app concept to work :) > > Does anyone have a working example of this?Are you sure that the default value of your column is not 0 (see David''s comment above)? CocoaMySQL has an annoying habit to declare "default 0" to integer type columns even if you don''t specify it yourself. I''ve got caught by this myself a few times. //jarkko -- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 10, 2004, at 10:47 PM, Jarkko Laine wrote:> Stefan, > > On 10.12.2004, at 23:41, Stefan Arentz wrote: > >> >> On Dec 10, 2004, at 11:54 AM, David Heinemeier Hansson wrote: >>> Beware, that if you set the default column value to 0, then it''ll >>> initialize the field with a 0. >>> >>> (I really should get Rails 0.9, shouldn''t I :)?) >>> >>> ...also, render_action "add" just renders the template. It doesn''t >>> call CountriesController#add. >> >> I upgraded to the beta-gems but it still behaves the same. I feel >> pretty stupid not getting such a simple web app concept to work :) >> >> Does anyone have a working example of this? > > Are you sure that the default value of your column is not 0 (see > David''s comment above)? CocoaMySQL has an annoying habit to declare > "default 0" to integer type columns even if you don''t specify it > yourself. I''ve got caught by this myself a few times.I could live with the default value. The problem is that when I submit the form with the integer field set to ''1234a'' that it resets back to 0. Because the validation kicked in and rejected the value. I would rather keep the original value and show an appropriate error. I''m looking at the rails code but it is a bit difficult to see where the value gets lost. S.