为什么需要此视图模板? [英] Why is this view template expected?

查看:65
本文介绍了为什么需要此视图模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器方法来验证已收到带有令牌的链接的用户(请参阅底部的方法).我有一个集成测试:

I have a controller method to authenticate a user who has received a link with a token (see method at the bottom). I have an integration test:

def test
  get login_path('invalid token')     // Login_path routes to the controller method below.
  assert flash[:danger]
  assert_redirected_to root_path
end

此测试产生以下错误(指的是get login_path('invalid token')):

This test produces the following error (referring to get login_path('invalid token')):

ActionView::MissingTemplate: Missing template invitations/login, application/login with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. 

视图invitiations/login确实不存在.但是,鉴于以下控制器方法,永远都不需要这样的视图(它总是重定向到root_path或呈现profiles/show).是什么原因导致此错误?

The view invitiations/login indeed doesn't exist. However, such a view should never be needed given the controller method below (it always either redirects to the root_path or renders profiles/show). What could be causing this error?

控制器方法:

def login
  inv = Invitation.where('email = ?', params[:email])
  if inv
    inv.each do |person|
      if person.authenticated?(:invitation, params[:id])
        @organization = person.organization
        unless @organization.nil?
          render 'profiles/show' and return
        else
          flash[:danger] = "Error"
          redirect_to root_path and return
        end
      end
      flash[:danger] = "Invalid link"
      redirect_to root_path
    end
  else
    flash[:danger] = "Invalid link"
    redirect_to root_path
  end
end

P.S.该测试过去一直通过,即直到我重写控制器方法以适应多个inv为止(请参阅使用find_by方法检索多个记录).

P.S. The test used to pass, i.e. until I rewrote the controller method to accommodate for multiple inv's (see Retrieve multiple records with find_by method).

推荐答案

您使用if inv-如果不存在带有匹配电子邮件的邀请,由于inv是ActiveRecord查询对象,它仍将返回true.但是each然后什么也不做,即没有重定向或显式呈现.默认渲染将被调用,并期望模板存在.

You use if inv - this will still return true if no invitations with matching email exist, since inv is an ActiveRecord query object. But then the each does nothing, i.e. does not redirect or render explicitly. Default render will be invoked and expect a template to exist.

使用if inv.present?将解决此问题.

(此外,您可能要确保inv集合仅包含一个结果.在同一请求中多次重定向或渲染将导致错误.)

(Also, you might want to make sure the inv collection only contains one result. Redirecting or rendering multiple times in the same request will result in an error.)

这篇关于为什么需要此视图模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆