1

Im trying to render a specific layout when the form is NOT properly filled:

Inside my views/layouts I've:

  • application.html.erb
  • empty.html.erb

My controller:

class UsersController < ApplicationController def new @user = User.new render layout: "empty" # I wan to re-use this layout is the user is not been saved end def create @user = User.new(user_params) if @user.save redirect_to sign_in_path else render :new end end def user_params params.require(:user).permit! end end 

The thing is that it works it re-renders the new-action when it's not true BUT it uses Application-default layout and NOT the other layout-file called: empty.html.erb

According to Rails guide lines it should work...but not for me :(

Thanks for your time

1 Answer 1

4

In the def create method, the render :new is rendering the new.html.erb view using the default layout. You can try

 render layout: "empty", template: "users/new" 

render merely "paints" the page according to the layout and template given to it.

redirect_to "new" will send a request for the new action.

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

5 Comments

Hi there! just one more question: Why did it work in the first call (inside the new-action=> render layout: "empty") and then in the create-action is requiring more method (template: template: "users/new" ). It's not very clear when I look at the wiki (guides.rubyonrails.org/layouts_and_rendering.html) for RoR.
There wasnt a "second" call to new. When you render :new, it doesn't call to the new action, it merely uses the renders (or draws) the new.html.erb and sends the resulting page to the browser.
"it merely uses the renders (or draws) the new.html.erb" but using application-layout as a default right?
Yes. render :new renders new.html.erb using the default view/layouts/application.html.erb. Note that the new action is not called. If you want the new action, use redirect_to "new".
I'm going your first suggestion " render layout: "empty", template: "users/new" ". If I use " redirect_to 'new' " I can't display the validation errors made by the user. It'll reload a new clean template :) thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.