0
votes

I'm building a simple login screen. I'd like to return an :internal_server_error to my client if there isn't a matching user or if the password is incorrect. However, I'm getting the following missing template error:

ActionView::MissingTemplate (Missing template login/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/Shelf/myGroceryList/app/views"

What has me confused is that I have an index.html.erb template in views/login. Thoughts?

Route:

  post '/login', to: 'login#show'

Controller(login_controller.rb):

class LoginController < ApplicationController
  protect_from_forgery with: :null_session

  def index

  end

  def show
    username = params.values.last["email"]
    password = params.values.last["password"]
    loginType = params.values.last["loginType"]

    if loginType == "Login"
      login(username, password)
    else
      createNewUser(username, password)
    end
  end

  def login(username, password)
    begin
      user = User.find_by(username: username)
      raise(StandardError) if user.password != password
    rescue
      render 'index', :status => :internal_server_error
    end
  end

  def createNewUser(username, password)
    User.new(username: username, password: password)
  end
end

View(index.html.erb):

<body><%= javascript_pack_tag 'login' %></body>
1
what i don't understand, is if you are hitting /login which routes to login#show, where is it attempting to render the index template? - max pleaner
Are using API routes? - Pavan
@max pleaner, inside the rescue. - Jonny B
@Pavan, this is more or less a rails api with a react front end. However, I'm not sure the routes are different between rails api and non api. If they are what are those differences? - Jonny B

1 Answers

1
votes

ActionView::MissingTemplate (Missing template login/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/Shelf/myGroceryList/app/views"

The error says it can't find the file with the name index and with the format json. As are you having an API, probably the request/response would be in the format json not html. That said you need to create a index.json.erb inside app/views/login to resolve the error.