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

萝らか妹 提交于 2019-12-10 13:53:06

问题


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!


回答1:


<%= 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



回答2:


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.



来源:https://stackoverflow.com/questions/9304368/rails-3-render-partial-from-another-controller-error-actionviewmissingtempla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!