为什么需要这个视图模板?

Why is this view template expected?

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

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')):

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)。什么可能导致此错误?

控制器方法:

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(参见 )。

您使用 if inv - 如果不存在具有匹配电子邮件的邀请,这仍然 return true,因为 inv 是一个 ActiveRecord 查询对象。但是 each 什么都不做,即不显式重定向或呈现。将调用默认渲染并期望模板存在。

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

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