Authlogic - Render Partial

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 (setup through a named route)
And this works: 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

On Sep 24, 3:06 pm, bui [email protected] 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?

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 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.

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?

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

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.