Layout not rendered when render_component used


#1

Hi

I’ve got a login action that handles authenticating users, setting
cookies and, via its view, displaying an HTML snippet containing a form
with username and password fields. It’s called from the main
application.rhtml layout so I don’t need to have a seperate page (away
from the main site) with a login form.

My user model is more-or-less the same as in the Agile RoR book.

app/controllers/auth_controller.rb:

class AuthController < ApplicationController
layout false

def login
if request.get?
@user = User.new
else
@user = User.new(@params[:user])
logged_in_user = @user.try_to_login
if logged_in_user
# handle setting cookies, setting session
# and redirecting to index
else
flash[:loginerror] = ‘Invalid username/password combination’
end
end
end
end

app/views/login.rhtml

<% if flash[:loginerror] %>

<%= flash[:loginerror] %>

<% end %> <%= form_tag :controller => 'auth', :action => 'login' %> name: <%= text_field 'user', 'name' %>
password: <%= text_field 'user', 'password' %>
<%= end_form_tag %>

app/views/layouts/applications.rhtml:

<% if session[:user] %>

Hi, <%= session[:user].name %>.

<% else %> <%= render_component :controller => 'auth', :action => 'login' %> <% end %>

When I GET an action that should be rendered with the application.rhtml
layout the login form appears with no problems. After submitting the
form (by POST), only the login view is rendered. No layout.

If I don’t include the “layout false” line in AuthController the whole
thing gets stuck in a loop as the view calls the layout and the layout
calls the view, etc.

What am I doing wrong here? How can I get the output of the combined
auth action and view to be inserted into the main layout? Is this even
the right way of doing things? It feels like a reasonable approach but
I’m prepared to be told I’m completely wrong.

Thanks in advance,

Mark Drayton