1

I'm getting

object index is not a member of package views.html

but unlike all the other threads opened on this subject, my issue seems totally unrelated to the IDE. I get this issue from the command line (no IDE) no matter how much I try to clean and rebuild by running

activator clean compile run

or just

sbt clean compile

Here is my conf/routes:

GET      /                   controllers.Application.index
GET      /books              controllers.Application.listBooks
POST     /books              controllers.Application.upload

GET     /assets/*file        controllers.Assets.at(path="/public", file)

This si my views/index.scala.html:

@import play.api.data.Form
@import models.Book

@(form: Form[Book])(implicit messages: Messages)

<!DOCTYPE html>
<html>
    <head>
        <title>xxx</title>
        <link rel="stylesheet" type="text/css" media="screen" href='@routes.Assets.at("stylesheets/main.css")'>
        <script type="text/javascript" href='@routes.Assets.at("javascripts/jquery-1.9.0.min.js")'></script>
    </head>
    <body>

        <div class="screenshot">

            <div class="navbar navbar-fixed-top">
                <div class="navbar-inner">
                    <div class="container">
                        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
                        </a> <a class="brand" href="#">Workday Dublin CTF</a>

                        <div class="nav-collapse">
                            <ul class="nav">

                            </ul>
                        </div>
                    </div>
                </div>
            </div>

                <h1>All Your Books Are Belong To Us</h1>
                <div class="container">

                    <h2>Add Book</h2>
                    @helper.form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {

                        @helper.inputText(form("title"))
                        @helper.inputText(form("author"))
                        @helper.inputFile(form("myUpload"))

                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary">Create Book</button>
                        </div>
                    }

                </div>
        </div>
    </body>
</html>

Finally, here is where the error is thrown, in my controllers/Application.scala:

package controllers

import models.Book
import models.Book._
import play.api.mvc._

class Application extends Controller {

  def index = Action {
    Ok(views.html.index(Book.form))
  }

  def listBooks = Action {
    Ok(books.head.myUpload)
  }

  def upload() = Action(parse.multipartFormData) { request =>
    request.body.file("myUpload").map { myUpload =>
      import java.io.File
      val filename = myUpload.filename
      val contentType = myUpload.contentType
      myUpload.ref.moveTo(new File(s"/tmp/$filename"))
      addBook(Book("xxxtitle", "xxxauthor", filename))
      Ok("File uploaded at /tmp/"+filename)
    }.getOrElse {
      Redirect(routes.Application.index).flashing(
        "error" -> "Missing file")
    }
  }
}

The error is thrown at Ok(views.html.index(Book.form)) which references models/Book.scala:

package models

import play.api.data.Form
import play.api.data.Forms._

case class Book(title: String, author: String, myUpload: String)

object Book {

  var books = List(Book("title test 1", "author test 1", "filename test 1"))

  def addBook(book: Book) = books = books ::: List(book)

  val form = Form(mapping(
    "title" -> text,
    "author" -> text,
    "myUpload" -> nonEmptyText)(Book.apply)(Book.unapply))
}

As I've researched a lot about this, and no other solution has worked so far, any help would be immensely appreciated. Thank you so much!

iammyr
  • 271
  • 5
  • 13
  • 1
    this import `import views.html.index` is not required – Nagarjuna Pamu Aug 31 '16 at 15:18
  • Hi @pamu thanks. That was leftover from one of my attempts to solve the issue. I've removed it now but I still get the same error. (gonna update the original post, too) – iammyr Aug 31 '16 at 15:20
  • from where `(implicit messages: Messages)` is coming? There is no implicit defined by you anywhere or is it related to Internationalization ? – curious Aug 31 '16 at 15:27
  • try using `implicit req` inside action `def index = Action { implicit req => Ok(views.html.index(Book.form)) }` – Nagarjuna Pamu Aug 31 '16 at 15:32
  • @curious I had to add that in order to avoid the following error "index.scala.html:39: could not find implicit value for parameter messages: play.api.i18n.Messages [error] @helper.inputText(form("author"))" – iammyr Aug 31 '16 at 15:56
  • @pamu thanks a million but unfortunately I'm still getting the same error even after applying your suggestion (and doing activator clean compile run) – iammyr Aug 31 '16 at 15:57
  • can you try moving the import statements below the `@(form)` args declaration in the template? – tksfz Aug 31 '16 at 16:41
  • thanks @tksfz but no, that didn't help – iammyr Aug 31 '16 at 16:58

2 Answers2

2

Delete all content from folder /project/target and build on terminal: sbt compile

lucasddaniel
  • 1,779
  • 22
  • 22
1

Apparently, even in this case the issue was related to the IDE. Despite seemingly unrelated, given it occurred from the command line and even after applying all the changes suggested by others.

I created a new project -> Play 2.x in IntelliJ IDEA 2016.2.2. Then I copy/paste all the content from the old faulty project, within in the new project structure. And everything worked straight away, although I had to add

import play.api.Play.current
import play.api.i18n.Messages.Implicits._

to Application.scala in order to pass the implicit messages (as @curious spotted in the comments). I did not mark his comment as the answer because when I tried to add these imports in the faulty project, I was still getting the same error. So nope, the fix would be something to do wth the IDE settings which I still am not too sure about. Surely my workaround is good enough for me, though.

Community
  • 1
  • 1
iammyr
  • 271
  • 5
  • 13