# Authlogic - Render Partial

**URL:** <https://www.ruby-forum.com/t/authlogic-render-partial/176118>\
**Category:** Rails\
**Created:** [September 24, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118 "2009-09-24T23:25:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 24, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/1 "2009-09-24T23:25:33Z")

</div>

New to Rails. Using Authlogic for authentication and went through  
tutorial at github. Having a bit of a problem getting the login  
screen to render as a partial from a different view file.

I have a index.html.erb view file for the “home” controller.

I tried \<%= render :partial =\> “user\_sessions/user\_session” %\>

but it fails with this error:

“Called id for nil, which would mistakenly be 4 – if you really  
wanted the id of nil, use object\_id”

user\_sessions is the view directory for the login form created through  
the Authlogic tutorial. I created a partial called  
\_user\_session.html.erb which contains the entire login form and looks  
like this:

\<% form\_for @user\_session, :url =\> user\_session\_path do |f| %\>  
\<%= f.error\_messages %\>  
\<%= f.label :email %\>  
  
\<%= f.text\_field :email %\>  
  
  
  
\<%= f.label :password %\>  
  
\<%= f.password\_field :password %\>  
  
  
  
\<%= f.check\_box :remember\_me %\>\<%= f.label :remember\_me %\>  
  
  
  
\<%= image\_submit\_tag(“/images/Login Button.jpg”) %\>  
\<% end %\>

user\_sessions/new.html.erb now looks like this

\<%= render :partial =\> ‘user\_session’ %\>

And this works: [http://mysite/login](http://mysite/login) (setup through a named route)  
And this works: [http://mysite/user\_sessions/new](http://mysite/user_sessions/new)

However, trying this \<%= render :partial =\> “user\_sessions/  
user\_session” %\> in the home/index.html.erb view fails.

Can anyone help with this? I seem to be missing something really  
fundamental here.

Thanks

---

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 25, 2009, 1:03am UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/2 "2009-09-25T01:03:19Z")

</div>

On Sep 24, 3:06 pm, bui [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> "Called id for nil, which would mistakenly be 4 – if you really  
> \<%= f.text\_field :email %\>
> 
> Thanks

A guess, as I don’t have the full backtrace and don’t know what the  
controller that passes to the view that calls the partial rendering  
does: is the @user\_session instance variable nil?

---

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 25, 2009, 7:56am UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/3 "2009-09-25T07:56:38Z")

</div>

The controller is the “home” controller and looks like this (very  
basic):

class HomeController \< ApplicationController  
def index  
end  
end

The view for the home controller is is just a basic home page and I  
just wanted to render a login partial there since I’m also planning to  
use it elsewhere in the app.

I assumed that there is no user\_session, since a user is not logged in  
at this point. So, I didn’t think I would need to pass it anything.  
I can hit [http://mysite/lgoin](http://mysite/lgoin) without passing any instance variables  
and it shows the partial properly.

Here’s a trace:

Showing app/views/user\_sessions/\_user\_session.html.erb where line #1  
raised:

Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Extracted source (around line #1):

1: \<% form\_for @user\_session, :url =\> user\_session\_path do |f| %\>  
2: \<%= f.error\_messages %\>  
3: \<%= f.label :email %\>  
  
4: \<%= f.text\_field :email %\>

Trace of template inclusion: app/views/home/index.html.erb

RAILS\_ROOT: c:/wwwroot/ror/blostm  
Application Trace | Framework Trace | Full Trace

C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
record\_identifier.rb:76:in `dom_id' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/ helpers/record_identification_helper.rb:16:in `dom\_id’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action\_view/  
helpers/form\_helper.rb:293:in `apply_form_for_options!' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/ helpers/form_helper.rb:277:in `form\_for’  
c:/wwwroot/ror/mysite/app/views/user\_sessions/\_user\_session.html.erb:  
1:in  
`_run_erb_app47views47user_sessions47_user_session46html46erb_locals_object_user_session' c:/wwwroot/ror/mysite/app/views/home/index.html.erb:4:in `\_run\_erb\_app47views47home47index46html46erb’

By the way, thanks for responding to my post.

---

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 25, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/4 "2009-09-25T20:13:05Z")

</div>

pabcas - Thanks for responding to my question. I really appreciate  
the help. Changing my partial to use “UserSession.new” seemed to  
work. The “home” page was able to render properly.

I checked the user\_sessions\_controller.rb file and I have this method  
defined.

def new  
@user\_session = UserSession.new  
end

Did you mean the home\_controller.rb file?

---

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 25, 2009, 10:07am UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/5 "2009-09-25T10:07:10Z")

</div>

Try \<% form\_for UserSession.new … if you want the form to work  
anywhere, or make sure that @user\_session is set in the controller,  
eg. @user\_session = UserSession.new

Paul

---

<div class="post-metadata">

**Author:** ![bui](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@bui](https://www.ruby-forum.com/u/bui)\
**Post date:** [September 25, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/authlogic-render-partial/176118/6 "2009-09-25T22:19:31Z")

</div>

Paul - Just an update. Although changing the partial as you suggested  
allowed the “home” page to render properly, I think it also kept  
Authlogic from working properly.

When the partial contains this:  
\<% form\_for UserSession.new :url =\> user\_session\_path do |f| %\>

I get nothing from Authlogic when I hit the Login button with no  
credentials entered.

But when I revert to its original form:

\<% form\_for @user\_session, :url =\> user\_session\_path do |f| %\>

Authlogic gives me the following response:

* * *

1 error prohibited this user session from being saved

There were problems with the following fields:

```
* You did not provide any details for authentication.

```

* * *

So here is how I was able to get it working:

- Changed “home” controller as follows:

def index  
@user\_session = UserSession.new  
end

- Changed view for “home” controller (i.e. - index.html.erb) as  
follows:

\<%= render :partial =\> “user\_sessions/user\_session”, :locals =\>  
{ :user\_session =\> @user\_session } %\>

This seems to work.

Thanks for pointing me in the right direction.
