views:

30

answers:

4

In Rails how do I call a form from another model in any given layout? I have a login form I want to put in the header of every page. I created a partial with the following in it:

<% form_for(@user_session) do |f| %>

  <p>
    <%= f.label :username %><br />
    <%= f.text_field :username, :class=>'' %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>

    <%= f.check_box :remember_me %><%= f.label :remember_me %><br />
  <br />

  <p>
    <%= f.submit 'Login' %>
  </p>
<% end %>

then tried calling that partial in my header and that doesn't seem to work.

<%= render :partial => 'user_sessions/login' %>

I get a "Called id for nil" error

A: 

Why not just <%= render @user_session %> ?

Shripad K
+1  A: 
<%= render :partial=> 'user_sessions/login' :layout => false%>

Use this

I think you save your partial with '_login.rhtml' like ?

@sachinrathore11: True if he has included a layout which checks for existence of a logged in user! Nice answer.
Shripad K
same error as above
Nick Faraday
A: 

Is the @user_session variable set up within all of your controller actions?

John Topley
I belive so... I'm following the http://github.com/binarylogic/authlogic_example, what I want to do is have a login form in the header of each page if the user is not logged in.
Nick Faraday
A: 

Got the answer from jmesserer over at railsforum, just needed to change:

<% form_for(@user_session) do |f| %>

to

<% form_for UserSession.new do |f| %>
Nick Faraday