5

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!

botbot
  • 7,299
  • 14
  • 58
  • 96
  • where is your partial exactly located ? And, why are you assigning a routes path in your render ? – prasvin Feb 16 '12 at 06:00
  • the partial is in views/sessions/new.html.erb. the reason why i'm using '/login' in my call to render is because i figured it would render the views/sessions/new.html.erb partial. – botbot Feb 16 '12 at 06:15

2 Answers2

9

<%= 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
prasvin
  • 3,009
  • 23
  • 28
  • i updated my post to show my sessions controller code. i'm sorta new at this so, i don't really understand why i would have to pass along a :@user => User.new? is that so the line: if user = User.authenticate(params[:email], params[:password]) won't fail in the session controller? i was following a tutorial where i had to add a new.html.erb to my /sessions folder, but now that i look at my controller, it only has a create and a detroy method. kinda confusing. – botbot Feb 16 '12 at 07:11
  • If u are trying to create some sort of authenticating system with this, I really recommend using the Devise gem https://github.com/plataformatec/devise . Its super easy and very very helpful. It creates the session by itself, provide useful helpers(such as current_user) and get us free from managing the session hassle. I would rather leave the authenticating part to Devise. That way if the user is not authenticated, Devise will redirect it to the sign in page. Now, you can design the sign in page as u desire and render login (username / password) wherever you want. – prasvin Feb 16 '12 at 07:50
  • 1
    Now, going straight to your question, I suggest creating a partial `_login.html.erb` in views/sessions. Then, render the login partial as above. The reason `@user` is passed is because the partial has a form for `@user`, so its instantiated with `User.new`. Populate the partial with form for user, as shown above, containing email, passwords and, if requires, some other fields. When the form is submitted the params are passed to sessions#create becoz of the url specified in the form. The code is untested, but should help. But, please hv a look at Devise first & use if it fits ur requirements. – prasvin Feb 16 '12 at 08:06
  • 1
    prasvin, thanks for your suggestions. i will look into devise as you suggested. but i probably won't open up that can of worms right now, i think the sessions for this app are basic enough and work well. i have a logged_in? function that checked the user's session, if they don't have a session, they are sent to the login page. – botbot Feb 16 '12 at 08:56
  • 2
    i will definitely try your suggestion now to see how that goes. if you don't mind adding a +1 to my initial comment, i may receive enough reputation points to upvote your comments. thanks again! – botbot Feb 16 '12 at 08:57
  • i mean, i will try implementing the _login partial as you suggested. – botbot Feb 16 '12 at 09:04
0

Check your file extension in my case file extension was rhtml, I changed it into html.erb.

Now its working fine.

Note:

This file with rhtml extension was working fine in rails <= 3.0.10. But stopped working in rails 3.1.12. So I changed its extension as mentioned above.

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53