3从另一个控制器渲染部分错误

3从另一个控制器渲染部分错误

本文介绍了Rails 3从另一个控制器渲染部分错误(错误:ActionView :: MissingTemplate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的application.html.erb的标题中包含一个登录名(用户名/密码).我收到此错误:

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"

当我在application.html.erb中进行此调用时,就会发生这种情况:

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

<%= render '/login' %>

'/login'在我的route.rb中定义为:

'/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

我在其他帖子中看到这可能是由于未在控制器操作中定义变量,但由于这是一个会话,并且位于application.html.erb(application_controller.rb)中,不知道该怎么做.有人知道该怎么做吗?谢谢!

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!

推荐答案

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

将呈现会话视图的登录部分,即视图/会话中的"_login.html.erb",并将@user实例化为新用户,以便可以在部分视图中直接将其引用为:

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

这篇关于Rails 3从另一个控制器渲染部分错误(错误:ActionView :: MissingTemplate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:39