1

When I use a scaffold to generate a Foo model/controller, my controller has a default create action that will render :new if @foo.save returns false.

What happens to my @foo object when I render :new?

My stock new.html.erb view refers to @foo.errors, which makes me think that it reuses the @foo from my create action (rather than create a new object with @foo = Foo.new again in the new method). Is this not the case?

I'm worrying about this because I want to reuse this convention in a new namespaced controller (everything else appearing equal), but I get nil for @foo when render :new happens. It appears it's neither reusing the Foo object, nor creating a new one. So I'm trying to understand render better.

2 Answers 2

3

render doesn't call the action new, it just processes the file new.html.erb (or whatever) and sends it to the browser. The @foo instance variable created in your create action is used when rendering that template, that's why, if the record fails to save, the when the form is rendered the user's information is still present in the form fields (assuming the form is set up correctly).

If you want to start a whole new request with a fresh @foo instance variable, you can redirect_to :new instead of rendering, or just manually set @foo = Foo.new before the call to render.

If you have a 'new namespaced controller', that's (self-evidently) not the same controller. It's not clear exactly what you're doing from the question, but if you're setting @new in one controller and then trying to access it from another controller... that's never going to work.

Sign up to request clarification or add additional context in comments.

Comments

1

From the documentation

If you want to render the view that corresponds to a different template within the same controller, you can use render with the name of the view:

So, render is used to render the view template and does not actually call the action. The name of the view to render can either be a string or symbol.

render 'new' # renders new.html.erb

render :new  # renders new.html.erb

So, in your case, when render :new in the create action is executed, all the controller does is renders the new.html.erb passing on the instance variable @foofrom the create action. That is why you're able to see the @foo.errors.full_messages in the view when saving a record fails because @foo refers to the invalid object.

In short, render just renders the template you specify and does not call an action recreating an object.

The parameter that you pass to the render method need not be the action name. You can also pass the path to the actual view file.

For the whole list of valid arguments, see section 2.2.4 in http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.