Michael
Michael

Reputation: 11

Redirect in Scala play framework

I have a problem with Redirect in Scala play framework.

How can I redirect to view BooksController.index() ? In documentation they suggest to use Redirect but I don't know how.

def edit(Id: Int) = Action {
    val book: Book = Book.findById(Id)
    Ok(views.html.edit())
  }

  def update = Action {
      implicit request =>
      val (id, title, price, author) = bookForm.bindFromRequest.get
      val book: Book = Book.findById(id)
      book.id = id
      book.title = title
      book.price = price
      book.author = author
      Redirect(routes.BooksController.index())
  }

Now can recognize --> import play.api.mvc.Results._ But i have an error --> "object java.lang.ProcessBuilder.Redirect is not a value"

Upvotes: 0

Views: 2921

Answers (2)

Dmitrii Sidenko
Dmitrii Sidenko

Reputation: 658

If you would really like to continue using reverse routing in your code instead of having string uri values all over the place, see this: Redirect with Bad Request Status. The Redirect function accepts only a String or Call.
Try the following steps:
0) Add in the BookController

import play.api.mvc._

1) Add the following string in your route config file(hard disk location: controllers/BooksController)

GET  /redirectedPage  controllers.BooksController.index

2) Define a variable in the BookController

val Home = Redirect(routes.BookController.index())

3) Describe in the BookController

def update = Action {
    implicit request => Home
}

Also do "sbt clean; sbt compile" to recompile auto-calls in ReverseRoutes.scala.
Well done.

Upvotes: 1

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

The last line in the update action is the Redirect call which redirects to BooksController index route

import play.api.mvc.Results._

Redirect(routes.BooksController.index())

Upvotes: 0

Related Questions