Scala and Action redirect

243 views
Skip to first unread message

Loic

unread,
Apr 11, 2011, 10:57:32 AM4/11/11
to play-fr...@googlegroups.com
Hello,

I'm trying to do an action redirect in my play controller. I use Scala and play 1.2RC3

The code looks like this:

def list() = {
    Template("albums" -> Albums.findAll())
  }

 def save(@Valid album: Album) {
    if (Validation.hasErrors) Action(form())
    album.save
    Action(list)
  }


The list method works fine. But not if I come from the save method. Action(list) seems to do nothing, in my browser it just take me to a blank page. There's no error in the console, and if i put a break point into the list method, it never goes inside when I use save.
Action(form()) does nothing too...

Did I miss something?
I didn't find any documentation on this part of the framework

Thanks, 
Loic



Guillaume Bort

unread,
Apr 11, 2011, 11:08:06 AM4/11/11
to play-fr...@googlegroups.com
You save method returns Unit. It should be written as:

def save(@Valid album: Album) = {
if (Validation.hasErrors) {
Action(form())
} else {
album.save
Action(list)
}
}

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

--
Guillaume Bort, http://guillaume.bort.fr

Loic

unread,
Apr 11, 2011, 11:35:55 AM4/11/11
to play-fr...@googlegroups.com
Oh yes it works now, sorry for that I just forgot the '=' symbol...

Thanks very much!

v6ak

unread,
Apr 11, 2011, 3:19:35 PM4/11/11
to play-framework
It is a common mistake in early phases of switching to Scala. Note
that many functions can be written in the following way:

def foo(args) = anExpression

Using the "=" symbol looks natural there. Whenever you are using the
"= { ... }" construct, you are using something more general. For
example, following code calculates roots od a quadratic equation.
Variables d, denom, dSqrt aren't exposed outside this code:
val roots = {
val d = b*b-4*a*c
val denom = 2*a
val dSqrt = Math.sqrt(d)
(
(-b+dSqrt)/denom,
(-b-dSqrt)/denom
)
}

So, you can use both of these concepts together:

def roots(a: Double, b: Double, c: Double) = {
val d = b*b-4*a*c
val denom = 2*a
val dSqrt = Math.sqrt(d)
(
(-b+dSqrt)/denom,
(-b-dSqrt)/denom
)
}

Why am I writting it? The first time I've realized this, it was
annoying. But later, I've found there something that makes sense.

By the way, you should write album.save() instead of album.save. There
is AFAIK a convention that the braces are ommited if and only if the
call has no side effect.

Regards
Vít Šesták 'v6ak'

Loic

unread,
Apr 12, 2011, 9:23:02 AM4/12/11
to play-fr...@googlegroups.com
Ok thanks a lot for this clarification! 

I have another question about redirect in the scala version of play.

Is there any mway to do something like this ? 

 public static void form(Long id) {
        Album album = Album.findById(id);
        render("@Application.form", album);
    }

From an Admin controller, I want to forward to the form.html template of the Application controller, with extra action (edit album is for admin only)

I don't want to do Template("album"->album) because it won't find the html template, i'm not in the right controller for that.
I don't want to do Action(Application.form(id)) because there is no method for this in Application controller (in the Application controller, it just display the template for creation, so there is no id parameter) 

I just want avoid to duplicate the same form.html file in Application and Admin folders.

With play/java it is possible doing "render("@Application.form", album)"

Is it possible in Scala?


Thanks!!

Loic

Guillaume Bort

unread,
Apr 12, 2011, 9:43:37 AM4/12/11
to play-fr...@googlegroups.com
Template("@Application.form", 'album -> album)

It should work we the trunk version.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Loic

unread,
Apr 12, 2011, 3:50:17 PM4/12/11
to play-fr...@googlegroups.com
Great, thanks very much!
Reply all
Reply to author
Forward
0 new messages