0

我希望我的 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 函数并保存在我的数据库中?

4

1 回答 1

1

如果用户名在会话中(就像withUser在其他函数中使用的那样),您无需做任何事情,它将随着每个请求自动传递。

不过,您需要做的是恢复它。看来您可以将您Action的另一个电话包装起来withUser,这样就可以了。

于 2012-09-04T11:02:38.937 回答