Following a simple example of the Rails book and using scaffolding I got
simple application to manage a user database.
the controller is like:
@user = User.new
@text = “Hello World”
@user = User.new(params[:user])
flash[:notice] = ‘User was successfully created.’
redirect_to :action => ‘list’
render :action => ‘new’
Note the @text var I create on the new action… The view is the scaffold
generated one with a little addition at the end to print the @text
var I create in the “new” action.
very simple stuff and it works.
If I add validation to the model like “validates_presence_of :name”
press the create button without filling the name field in the form the
action is rendered with the error box at the top indicating error (this
ok) but the @text var dissappears. Making some tests I find that @text
nil when this page is rendered and there are error_messages_for user.
If I want the @text var to be displayed in the form when creating a new
and when there are errors, I have to create that var in both actions
and “create” . I think this is a violation of the DRY principle.
Am I doing something wrong??