Authlogic: rendering the login form as a partial on every page of my site

Hi,

I'm trying to setup the Authlogic gem. I've followed this tutorial:
(because I'm using Rails 3)
http://www.logansbailey.com/2010/10/06/how-to-setup-authlogic-in-rails-3/

All of this works. But now I'd like to render the login form as a
partial on every page of my site.

1) I've rendered the partial in my application.html.erb file as
followed:

<%= render :partial => "user_sessions/form" %>

2) When I start my server and try rendering my index view of my home
controller, I get the following error:

undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(@user_session) do |f| %>
2: <% if @user_session.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@user_session.errors.count, "error") %>
prohibited this user_session from being saved:</h2>

3) I figured out I had to make a new @user_session var in the action
method of my controller for every view I'd like to render my login
form partial on.

4) I've put @user_session = UserSession.new in the "new"-action-method
in my home controller and so my index view rendered fine. But now I'd
like to render my login form on every page of my site.

Is there a way to set the @user_session for every action? Like in the
application_controller? How would you do that?

Thank you,
Mathew

Hi,

I’m trying to setup the Authlogic gem. I’ve followed this tutorial:

(because I’m using Rails 3)

http://www.logansbailey.com/2010/10/06/how-to-setup-authlogic-in-rails-3/

All of this works. But now I’d like to render the login form as a

partial on every page of my site.

  1. I’ve rendered the partial in my application.html.erb file as

followed:

<%= render :partial => “user_sessions/form” %>

  1. When I start my server and try rendering my index view of my home

controller, I get the following error:

undefined method `model_name’ for NilClass:Class

Extracted source (around line #1):

1: <%= form_for(@user_session) do |f| %>

2: <% if @user_session.errors.any? %>

3:

4:

<%= pluralize(@user_session.errors.count, “error”) %>

prohibited this user_session from being saved:

  1. I figured out I had to make a new @user_session var in the action

method of my controller for every view I’d like to render my login

form partial on.

  1. I’ve put @user_session = UserSession.new in the “new”-action-method

in my home controller and so my index view rendered fine. But now I’d

like to render my login form on every page of my site.

Is there a way to set the @user_session for every action? Like in the

application_controller? How would you do that?

You should able to do this in the application_controller:

before_filter :new_user_session

private

def new_user_session
@new_user_session = UserSession.new

end

Try that, should then have the @new_user_session available to you since all controllers inherit from application controller.

thanks!
works like a charm :slight_smile:

mattyh88 <mathew.hucks@...> writes:

thanks!
works like a charm :slight_smile:

> > Hi,
>
> > I'm trying to setup the Authlogic gem. I've followed this tutorial:
> > (because I'm using Rails 3)
> >http://www.logansbailey.com/2010/10/06/how-to-setup-authlogic-in-rail
>
> > All of this works. But now I'd like to render the login form as a
> > partial on every page of my site.
>
> > 1) I've rendered the partial in my application.html.erb file as
> > followed:
>
> > <%= render :partial => "user_sessions/form" %>
>
> > 2) When I start my server and try rendering my index view of my home
> > controller, I get the following error:
>
> > undefined method `model_name' for NilClass:Class
> > Extracted source (around line #1):
> > 1: <%= form_for(@user_session) do |f| %>
> > 2: <% if @user_session.errors.any? %>
> > 3: <div id="error_explanation">
> > 4: <h2><%= pluralize(@user_session.errors.count, "error") %>
> > prohibited this user_session from being saved:</h2>
>
> > 3) I figured out I had to make a new @user_session var in the action
> > method of my controller for every view I'd like to render my login
> > form partial on.
>
> > 4) I've put @user_session = UserSession.new in the "new"-action-method
> > in my home controller and so my index view rendered fine. But now I'd
> > like to render my login form on every page of my site.
>
> > Is there a way to set the @user_session for every action? Like in the
> > application_controller? How would you do that?
>
> You should able to do this in the application_controller:
>
> before_filter :new_user_session
>
> private
>
> def new_user_session
> @new_user_session = UserSession.new
> end
>
> Try that, should then have the @new_user_session available to you since all
> controllers inherit from application controller.
>
>
>
>
>
> > Thank you,
> > Mathew
>
> > –
> > You received this message because you are subscribed to the Google Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonrails-talk@…
> > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe@…<rubyonrails-talk%2Bunsubscrib e <at>

googlegroups.com>

> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.

I am trying to do the same thing. I followed the same tutorial, added the

@user_session = UserSession.new

(tried both the home controller and application controller)

but I am still getting

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: = form_for @user_session do |f|
    2: -if @user_session.errors.any?
    3: #error_explanation
    4: %h2= "#{pluralize(@user_session.errors.count, "error")} prohibited
this user_session from being saved:"
  app/views/user_sessions/_form.html.haml:1:in
`_app_views_user_sessions__form_html_haml__892280163_96031200'
  app/views/home/index.html.haml:6:in
`_app_views_home_index_html_haml__1051594895_96219820'

Any idea how to solve this? Everything else works fine, as described in the
tutorial. I am just unable to render the partial user_sessions/form from
anywhere else, i.e. it's only working from user_sessions/new.

I'd appreciate any help, I've been struggling with this for a while and cannot
figure it out.