我希望我的 Web 应用程序中的用户发表帖子。该帖子将包含他的用户名,以表明他发布了该帖子。我该怎么做?
更新!
这是我的工作代码
请在下面查看我的代码:
def createlisting = isAuthenticated { username => implicit request =>
Ok(html.createlisting(listingsForm))
}
def postlisting = withUser {user => implicit request => {
listingsForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.createlisting(formWithErrors)),
listing => {
val username = val username = User.getUserName(user)
Listings.create(listing.categorytype, listing.title, listing.description, username)
Redirect(routes.Application.active).flashing("success" -> "Listing %s has been posted".format(listing.title))
}
)
}
}
val listingsForm = Form(
mapping(
"_id" -> ignored(new ObjectId()),
"categorytype" -> mapping(
"category" -> nonEmptyText,
"subcategory" -> nonEmptyText)(Type.apply)(Type.unapply),
"title" -> nonEmptyText,
"description" -> nonEmptyText,
"username" -> igrnored(String)
)(Listings.apply)(Listings.unapply)
)
scala.html
@(createForm: Form[Listings])
@import helper._
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }
<h1>Add a Listing</h1>
@form(routes.Application.postlisting()) {
<fieldset>
@inputText(createForm("categorytype.category"), '_label -> "Main Category")
@inputText(createForm("categorytype.subcategory"), '_label -> "Sub Category")
@inputText(createForm("title"), '_label -> "Title")
@inputText(createForm("description"), '_label -> "Description")
</fieldset>
<div class="actions">
<input type="submit" value="Post Add" class="btn primary" href="/"> or
<a href="/" class="btn primary">Cancel</a>
</div>
}
我该怎么做才能让用户名自动传递给 postlistings 函数并保存在我的数据库中?