Question about Playframework Routes

I have this in my applcation routes file:

GET /new Tweets.create POST /new Tweets.save 

And, in my opinion, I create this form:

 #{form @save()} ... #{/form} 

But once submit a form that will send me to /tweets/save , and not to /new . Any ideas how I can fix this? Thanks!

+4
source share
3 answers

If you have already tried the route below (this is the correct way to use routes)

 #{form @Tweets.save()} 

and it didn’t work, I think you might put your route in the wrong place. Make sure that it is at the top of the routes file, and not after the entire route. The routes file is processed in order, so if all traps are detected, this is used first, and your other route is ignored. The trick looks like

 * /{controller}/{action} {controller}.{action} 
+5
source

Try using

  #{form @Tweets.save()} 

I think it is suggested to use class names with method names.

EDIT:

How the routing of the playback platform works, you define some route as

 GET /clients Clients.index 

If the request meets the URI /clients , then it will be intercepted by Clients.index() . If you have other routing, such that

 GET /clients Clients.save 

Then the structure ignores this routing because /clients aready has a mapping. (Most likely, this leads to some error in the console or logging thread, check your logs.)

Therefore, you cannot make it work like this. I see you are requesting a backward mapping that will return the same URI for different methods. However, the structure aims to intercept requests so that it simply ignores your second routing.

Try to split the pages. Most likely, you want to make the same representations for two functions. You can do this without redirecting them to the same URI.

+1
source

I think (if I was not mistaken) that the problem is that you expect the wrong behavior.

As I understand it, you expect submit to go to Tweet.save () (POST method) and then go back to Tweet.create () (GET method), since both have the same path (/ new).

In fact, Play calls the Tweet.save () function and waits for rendering at the end of Tweet.save () to display some result. If you want to redirect to Tweet.create (), you can make a call to this method at the end of the implementation of Tweet.save (), or with:

 create(<params>); 

or

 render("@create", <params>); 

and this should redirect (through 302) to the GET version.

0
source

All Articles