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.
class AuthController < ApplicationController
@user = User.new
@user = User.new(@params[:user])
logged_in_user = @user.try_to_login
# handle setting cookies, setting session
# and redirecting to index
flash[:loginerror] = ‘Invalid username/password combination’
<%= flash[:loginerror] %><% end %> <%= form_tag :controller => 'auth', :action => 'login' %> name: <%= text_field 'user', 'name' %>
password: <%= text_field 'user', 'password' %>
<%= end_form_tag %>
<% 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,