【问题标题】:Play! Scala Template Engine and Sessions玩! Scala 模板引擎和会话
【发布时间】:2012-08-28 20:29:22
【问题描述】:

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

【问题讨论】:

    标签: scala mongodb playframework playframework-2.0 salat


    【解决方案1】:

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

    不过,您需要做的是恢复它。看来您可以将您的 Action 包装在另一个对 withUser 的调用中,这样就可以了。

    【讨论】:

    • html 文件的语法是什么?目前我所拥有的不正确。
    • 您不需要任何与视图中的会话相关的东西,只需要在控制器中。您已经将用户名作为参数传递,如果您想这样做,这就是您所需要的。向他们展示。
    • 好的,有机会我会再测试的,非常尊重。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2013-04-12
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多