Forum: Ruby on Rails Layout not rendered when render_component used

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ff4602ff49ddafe339c70b582f5e325d?d=identicon&s=25 Mark Drayton (Guest)
on 2006-01-13 23:07
(Received via mailing list)
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

<div id="loginbox">
 <% if flash[:loginerror] %>
  <h3><%= flash[:loginerror] %></h3>
 <% end %>
 <%= form_tag :controller => 'auth', :action => 'login' %>
  <label for="user_name">name:</label>
  <%= text_field 'user', 'name' %><br />
  <label for="user_password">password:</label>
  <%= text_field 'user', 'password' %><br />
  <label for="login"></label>
  <input type="submit" name="login" id="login" value="go" />
 <%= end_form_tag %>
</div>

app/views/layouts/applications.rhtml:

<% if session[:user] %>
 <h3>Hi, <%= session[:user].name %>.</h3>
<% 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
This topic is locked and can not be replied to.