Damon Julian
Damon Julian

Reputation: 3989

playframework problem with routes:NoRouteFoundException

Trying out the custom editor portion in play tutorial,I created the routes as below

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/new                              Admin.save

GET      /admin?                                Admin.index
*        /admin                                 module:crud

and created the methods in Admin class

..
public static void form() {
        logger.info("Admin.form()");
        render();
    }       
    public static void save(String title,String content,String tags) {
        User author = User.find("byEmail",Security.connected()).first();
        logger.info("author="+author.getEmail());
        //create a post
        Post newPost = new Post(author,title,content);
        logger.info("new post="+newPost.getTitle()+" created");
        //set tags
        String[] tagArray = tags.split("\\s+");
        logger.info("tag array="+tagArray.length);
        for(String tag : tagArray ) {
            logger.info("tag="+tag);
            if(tag.trim().length() > 0) {
                newPost.getTags().add(Tag.findOrCreateByName(tag));
            }               
        }
        validation.valid(newPost);
        if(validation.hasErrors()) {
            logger.error("error in post");
            render("@form", newPost);
        }
        newPost.save();
        logger.info("new post saved");
        logger.info("going to index");
        index();

    }       
    public static void form(Long id) {
        if(id!=null) {
            Post post = Post.findById(id);
            render(post);
        }
        render();
    }
...

the views/Admin/index.html is

#{extends 'admin.html'/}
Welcome ${user}!! <span>you have written ${posts.size()?:'no'} ${posts.pluralize('post','posts')} so far </span>

#{list items:posts,as:'post' }
    <p class="post ${post_parity}">
        ${post_index}.<a href="@{Admin.form(post.id)}">${ post.title}</a>
    </p>
#{/list}
<p id="newPost">
    <a href="@{form()}"><span>+</span>write new post</a>
</p>

When I click on login ,the line containing the link "@{Admin.form(post.id)}" causes

Internal Server Error (500) for request GET /admin?

No route found (In /app/views/Admin/index.html around line 7)
No route able to invoke action Admin.form was found.

play.exceptions.NoRouteFoundException: No route found
    at play.templates.BaseTemplate.throwException(BaseTemplate.java:80)
    at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:237)
    at play.templates.Template.render(Template.java:26)
    at play.templates.GroovyTemplate.render(GroovyTemplate.java:184)
    at play.mvc.results.RenderTemplate.<init>(RenderTemplate.java:24)
    at play.mvc.Controller.renderTemplate(Controller.java:659)
    at play.mvc.Controller.renderTemplate(Controller.java:639)
    at play.mvc.Controller.render(Controller.java:694)
    at controllers.Admin.index(Admin.java:33)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:543)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:499)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:475)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:470)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:158)
    at Invocation.HTTP Request(Play!)

How do I solve this?Can someone help?Is there a problem in the order of paths in routes?The crud and secure modules are available as per the startup messages

update: the stacktrace as shown by play is here

application error page

I tried to change the order of entries in the routes file..Put the * /admin path before GET / admin? as below ..Now I am getting a strangely rendered page

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/new                              Admin.save

*        /admin                                 module:crud
GET      /admin?                                Admin.index

weird page This is where bob@gmail ends up when logged in! For the url http://localhost:9000/admin?,the page should be showing MyPosts link as selected..but here the Comments link is shown as selected..

So, it must be the problem with the order of paths in routes file...specifically that of * /admin? ..can someone tell me where exactly it should be put?

There was a slight problem in the routes file(a missing /) complete routes file is

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

#import crud routes

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/myPosts/{id}                     Admin.save
POST    /admin/new                              Admin.save

GET     /admin/?                                Admin.index
*       /admin                                  module:crud

# Home page
GET     /                                       Application.index

# details of a post
GET     /posts/{<[0-9]+>id}                             Application.showPost
GET     /captcha                                Application.captcha
GET     /posts/{tag}                            Application.taggedWith
POST    /posts/{<[0-9]+>id}/comments                    Application.postComment

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
# Import Secure routes
*       /                                       module:secure
*       /{controller}/{action}                  {controller}.{action}

The admin page is shown properly when I omit the Admin.form(post.id) link in index.html

#{list items:posts,as:'post' }
    <p class="post ${post_parity}">
        ${post_index}.<a href="#">${ post.title}</a>
    </p>
#{/list}
<p id="newPost">
    <a href="@{Admin.form()}"><span>+</span>write new post</a>
</p>

As soon as the link is added

#{list items:posts, as:'post'}
    <p class="post ${post_parity}">
        <a href="@{Admin.form(post.id)}">${post.title}</a>
    </p>
#{/list}

The No route able to invoke action Admin.form was found error message occurs

Upvotes: 1

Views: 4462

Answers (2)

Damon Julian
Damon Julian

Reputation: 3989

The confusion was caused because ,in the Admin class there were 2 form() methods ,one with no arg,another with id as arg.The no arg method was placed before the other..causing the router much confusion..

The problem was solved when method form() was replaced by method form(Long id)

Upvotes: 2

Codemwnci
Codemwnci

Reputation: 54884

Are you sure it is that route that is causing the problem? I have just tried this scenario and cannot recreate the error (although not using the CRUD and Secure modules), however as these routes come later in the routes file, these should not be causing issue.

I think it may be the following line of code causing issues.

 <a href="@{form()}"><span>+</span>write new post</a>

Specifically, this bit @{form()}

I think it should probably read

@{Admin.form()}

Upvotes: 0

Related Questions