I have a footer template which has a input and submit for email subscription. The footer is used by every page. When subscription succeeds, it will redirect back to current page. However, I found out I am passing a string value to indicate what current page is. What is the best way to have a footer template for Play 2.0 application?
footer.scala.html
@(page: String)
<div id="footer">
<div class="input-append">
<form action="@routes.ApplicationController.saveSubscription(page)"
method="post">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input class="btn" type="submit" value="Subscribe" />
</form>
</div> <!-- /input-append -->
</div> <!-- /footer -->
ApplicationController.java
public class ApplicationController extends Controller {
public static Result saveSubscription(String page) {
..........
flash("success", "success message");
if (page.equals("page1")) {
return redirect(routes.ApplicationController.page1());
} else if (page.equals("page2")) {
return redirect(routes.ApplicationController.page2());
}
}
}
page1.scala.html
@main("Page 1") {
<div>
<p>page 1</p>
</div>
@footer("page1")
}
page2.scala.html
@main("Page 2") {
<div>
<p>page 2</p>
</div>
@footer("page2")
}
EDIT 1
I follow @virtualeyes but it seems the subscribe.js is never been called. Here is the New setup.
main.scala.html
<html>
<head>
<script type="text/javascript" src="@routes.Assets.at("javascripts/jquery.min.js")"></script>
<script type="text/javascript" src="@routes.Assets.at("javascripts/vendor/jquery.validate.min.js")"></script>
<script src="@routes.Assets.at("javascripts/main.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/subscribe.js")" type="text/javascript"></script>
</head>
<body>
@footer()
</body>
</html>
footer.scala.html
<div id="footer">
<div class="input-append">
<form id="_form" action="@routes.ApplicationController.simpleSubscription()">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input id="_process" class="btn" type="submit" value="Subscribe" />
</form>
</div> <!-- /input-append -->
</div> <!-- /footer -->
routes
POST /subscribe controllers.ApplicationController.simpleSubscription()
Now I got his error:
Action not found
For request ‘GET /subscribe?emailAddress=fdsaf%40rte.com’
I am not sure if it is because method=”post” is removed. If I put it back, then the result will return but will redirect to /subscribe page. I also set a breakpoint at subscribe.js but it doesn’t seem to be called at all.
EDIT 2 – Working
after I changed a little bit for subscribe.coffee and get rid of main.coffee, now it’s working.
subscribe.coffee
$('#_process').click (e) ->
e.preventDefault()
isValid = $('#_form').validate().form()
if isValid
$('#_process').spin()
$.ajax
type: "POST"
url: $('#_form').attr('action')
data: $('#_form').serialize()
success: (data) ->
$('#_status > div').removeClass('alert-error').addClass('alert-success')
$('#_status > div').html( data )
$('#_status').fadeIn()
fade = () -> $('#_status').fadeOut('slow')
setTimeout fade, 2000
$('#_process').spin('stop')
error: (data) ->
$('#_status > div').removeClass('alert-success').addClass('alert-error')
$('#_status > div').html( data.responseText )
$('#_status').fadeIn()
fade = () -> $('#_status').fadeOut('slow')
setTimeout fade, 2000
$('#_process').spin('stop')
complete: () -> $('#_process').spin('stop')
footer.scala.html
<div id="_status">
<div class="alert alert-error"></div>
</div>
<div class="input-append">
<form id="_form" action="@routes.ApplicationController.simpleSubscription">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input id="_process" class="btn" type="submit" value="Subscribe" />
</form>
<div id="_spin"></div>
</div> <!-- /input-append -->
The spin() function is from https://github.com/pshizzle/spin.coffee
AJAX is the way™