Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6534747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:17:18+00:00 2026-05-25T10:17:18+00:00

I am getting started with rails, so this is a fairly basic question. I

  • 0

I am getting started with rails, so this is a fairly basic question. I am trying to render a login form (authlogic) in the homepage, using this code:

views/home/index.html.haml:

%p
  This is the home page...!

- if current_user
- else
  = render :template => 'user_sessions/new'

user_sessions_controller:

class UserSessionsController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => :destroy

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Login successful!"
      redirect_back_or_default user_controls_url
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = "Logout successful!"
    redirect_back_or_default home_url
  end
end

views/user_sessions/new.html.haml

= form_for @user_session, :url => {:action => "create"} do |f|
  = f.error_messages
  %div
    = f.label :login
    = f.text_field :login
  %div
    = f.label :password
    = f.password_field :password
  %div
    = f.check_box :remember_me
    = f.label :remember_me
  %div
    = f.submit "Login"

models/user_session.rb

class UserSession < Authlogic::Session::Base

  def to_key
    new_record? ? nil : [ self.send(self.class.primary_key) ]
  end

  httponly true
  secure true
end

When I visit the homepage, I get:

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: = form_for @user_session, :url => {:action => "create"} do |f|
2:   = f.error_messages
3:   %div
4:     = f.label :login
  app/views/user_sessions/new.html.haml:1:in `_app_views_user_sessions_new_html_haml___182031841_97682750'
  app/views/home/index.html.haml:6:in `_app_views_home_index_html_haml__679857083_97787190'

What am I doing wrong, and how do I fix it?

Thanks a lot for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:17:18+00:00Added an answer on May 25, 2026 at 10:17 am

    In your code, @user_session is being created when you visit the new action that is connected to user_sessions/new; it is not created when you go to the index action.

    When you render the user_sessions/new template from the index action, ERB/HAML is looking for an instance of @user_session and cannot find it, hence the error.

    So, you could instantiate @user_session like this:

    #Note: The <%%> is ERB code (please adjust it for the syntax used in HAML)
    <% @user_session = UserSession.new if @user_session.nil? %>
    
    = form_for @user_session, :url => {:action => "create"} do |f|
    ...
    

    Or, you can also do it in the index action itself, though it would be better to keep it out from the index action and instead do it as above (eg. what if you want to render the template from some other action as well – then you would be duplicating code unnecessarily)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, if this is a noobish question, but I'm just getting started with Rails
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
Im using the following guide for getting started with rails for ubuntu 9.10. http://guides.rails.info/getting_started.html
I am trying to help some people getting started programming on rails identify which
The Getting Started Rails Guide kind of glosses over this part since it doesn't
I am just getting started learning rails. I am building my first app using
I'm just getting started with Ruby on Rails development and I have a question
Just getting started using MVC in ASP.NET, I'm going to have it so users
I am just getting started with flex and am using the SDK (not Flex
I'm getting started building a site with Rails and I would like people to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.