Rails 3 render partial from another controller (error: ActionView::MissingTemplate)

botbot picture botbot · Feb 16, 2012 · Viewed 7.3k times · Source

I'm trying to include a login (username / password) in the header of my application.html.erb. I am getting this error:

Missing partial /login with {:handlers=>[:rjs, :builder, :rhtml, :erb, :rxml], :locale=>[:en, :en], :formats=>[:html]} in view paths "/app/views"

This is happening when I make this call in my application.html.erb:

<%= render '/login' %>

'/login' is defined in my routes.rb as:

match '/login' => "sessions#new", :as => "login" 

UPDATE: here is my sessions controller:

class SessionsController < ApplicationController

  def create 
    if user = User.authenticate(params[:email], params[:password])
        session[:user_id] = user.id
        user.last_login = Time.now
        user.save
        redirect_to root_path, :notice => "login successful"
      else 
        flash.now[:alert] = "invalid login / password combination " # don't show pass + params[:password]
        #render :action => "new"
        redirect_to login_path, :notice => "wrong user pass"
      end
  end

  def destroy 
    reset_session
      redirect_to root_path, :notice => "successfully logged out"
  end

end

I have seen in other posts that this can be due to not defining a variable in a controller action, but since this is a session, and it is in the application.html.erb (application_controller.rb), I'm not sure how to do this. Anybody know how to do this? Thanks!

Answer

prasvin picture prasvin · Feb 16, 2012

<%= render "sessions/login", :@user => User.new %>

will render login partial of sessions view, i.e. '_login.html.erb' in views/sessions and instantiate @user to new user so that it can be referenced directly in the partial as :

form_for @user, :url => sessions_path do |f| 
  f.text_field :email