Unable to resolve reverse routing methods in IntelliJ
Asked Answered
L

8

15

I'm following one of the play framework tutorials, but I'm getting compile errors whenever I try to use reverse routing. Firstly,

public static Result javascriptRoutes() {
    response().setContentType("text/javascript");
    return ok(
        Routes.javascriptRouter("jsRoutes",
            controllers.routes.javascript.Projects.add(),
            controllers.routes.javascript.Projects.delete(),
            controllers.routes.javascript.Projects.rename(),
            controllers.routes.javascript.Projects.addGroup()
        )
    );
}

where the error shown in intelliJ is 'cannot resolve method javascriptRouter(java.lang.String, ?, ?, ?, ?)'

But also in the a unit test:

@Test
public void notAuthenticated() {
    Result result = callAction(
            controllers.routes.ref.Application.index(),
            fakeRequest()
    );
    assertEquals(303, status(result));
    assertEquals("/login", header("Location", result));
}

where it cannot resolve the index method.

Is this a problem with intelliJ, or am I missing something within play?

For the first part, here is the entry in my routes file:

GET     /assets/javascripts/routes  controllers.Application.javascriptRoutes()

and my controller, Projects, has got the defined methods.

Liquidator answered 21/5, 2013 at 17:46 Comment(7)
Seems there are no such methods defined.Libation
I'm not sure if you're just being a smart-ass, but I edited my question to include a segment of the routes file. Also, my Application has an index method. I know because it renders the home page.Liquidator
Please link the Play tutorial where this code comes from.Merrie
playframework.com/documentation/2.1.1-RC2/JavaGuide6 This is part six or the tutorial. The relevant part is at the top.Liquidator
I have no problem, try ignoring the error at first, run play compile on your console an check IntelliJ again. Besides, a tip: write @Merrie somewhere in your comment so I get a notice you replied.Merrie
I'm having this same issue with my project in IntelliJ IDEA 12.1.13 and most new version of Play framework.Bebop
Having the same error "Cannot resolve method 'routes'", yet the server works perfectly as intended.Doubletongue
D
37

File -> Project Structure

Select Sources in Right Pane

Add Source folder

target/scala-XXX/classes_managed

target/scala-XXX/src_managed/main

Devoir answered 19/2, 2014 at 11:39 Comment(2)
Worked for me too. Similar issue and solutions in this response to IntelliJ IDEA Report Highlighting error when using routes in ControllerMortimer
The target directory is generated, right? Then, this fix would be undone every time the project is cleaned?Imbecility
A
22

I was running into the same problem and found the solution here: https://github.com/playframework/Play20/issues/969

In short:

  • Create the directories javascript and ref under the controllers package
  • Run activator compile and now Intellij should get it // used to be 'play compile'
  • If you still got the errors try to run activator idea again // used to be 'play compile'*
Albata answered 11/7, 2013 at 11:5 Comment(3)
Thanks a lot! I first run play idea and then created the folders, and the compile error was gone like magic. Not sure if the order matters.Celestecelestia
This is generally the right answer but a few clarifications: - Add the 'javascript' and 'ref' folders under /app/controllers - the 'play' command has been replaced by 'activator' My order of operations was 1) add directories, 2) activator idea, and 3) activator compile Doing a 'rebuild' in IntelliJ requires 'activator compile' to be run again. Annoying.Blois
Actually you should add javascript and ref dirs to target/scala-xxx/src_managed/main/controllersBolter
W
6

Pulled from a link provided by @Markus Kittig. Great temporary fix. https://github.com/playframework/playframework/issues/1784#issuecomment-26345523

Synopsis: Add target/scala-XXX as a managed source and remove the app controllers and views sources flag inside File->Project Structure->Modules->Sources. Then recompile.

Works on IntelliJ Ultimate 12.1.{4|6}. Created the play application with the command line interface and generated a project file using play idea. Used Play 2.2.0.

Wuhsien answered 29/10, 2013 at 2:21 Comment(0)
L
4

With IntelliJ 14.1 and Play 2.3.8 nothing of the above worked, but the advice from this mailing list worked. (Almost) blatantly copied:

Locate the target/scala-2.11/src_managed and target/scala-2.11/twirl directories in the project view, then right click and Mark Directory As -> Generated Sources (Root).

I bumped the scala version and obviously in newer versions of IntelliJ the Root word has been added. Also, you cannot select this from the Project Structure window, the option is not available. It is possible only through the Project pane in the main window. If it refuses to be marked as Generated Sources, try to unmark thetarget directory (Mark Directory As -> Unmark ).

Libidinous answered 8/4, 2015 at 13:30 Comment(0)
T
4

For people using Play 2.4.x or above, it seems that Play no longer produces reverse routing files for javascript in src_managed et al.

Instead, you need to include scala-2.xx/routes/main directory as Sources.

Thornhill answered 27/3, 2016 at 17:8 Comment(0)
T
1

This question was asked a year ago, but to answer for future queries by other coders, the problem is easily solved by adding a "play.Routes" path like this

public static Result javascriptRoutes() {
    response().setContentType("text/javascript");
    return ok(
            play.Routes.javascriptRouter("jsRoutes",

                    // Routes for Projects
                    controllers.routes.javascript.Projects.add(),
                    controllers.routes.javascript.Projects.delete(),
                    controllers.routes.javascript.Projects.rename(),
                    controllers.routes.javascript.Projects.addGroup()
            )
    );

}

Ensure that you have the proper imports to the class:

import play.mvc.*;
import play.data.*;
This answered 9/6, 2014 at 12:52 Comment(0)
A
0

I am using Idea 14.1.4 community edition, i managed to remove the index and route not resolved errors by right clicking on the target folder and marking it as not excluded. NB i run my project using the command line i can not find any run configuration in the ide.

Ancier answered 29/8, 2015 at 21:27 Comment(1)
If you have a NEW question, please ask it by clicking the Ask Question button. If you have sufficient reputation, you may upvote the question. Alternatively, "star" it as a favorite and you will be notified of any new answers.Beefburger
S
0

I didn't find that folders in my PLay 2.8 build, so as it was a problem with IntelliJ I found an easier solution:

https://mcmap.net/q/821513/-play-framework-tutorial-cannot-resolve-symbol-39-routes-39

Sarcasm answered 13/12, 2021 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.