Forum: Ruby on Rails Instance variables don't persist between actions?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Horacio S. (Guest)
on 2005-12-28 18:41
(Received via mailing list)
Following a simple example of the Rails book and using scaffolding I got
simple application to manage a user database.

the controller is like:

  def new
    @user =
    @text = "Hello World"

  def create
    @user =[: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??

Chris (Guest)
on 2005-12-28 19:00
(Received via mailing list)
When you hit the create button on your form, the create action is
called, not the new action.  When you get an error on your save, the
new form is rendered, but the new action isn't called so the @test
won't be set.

You can do "new" before you do your render of new as one workaround.

This topic is locked and can not be replied to.