2012-09-04 124 views
0

我想让我的Web应用程序中的用户发表帖子。该帖子将包含他的用户名,以表明他发布了它。我将如何做到这一点?玩! Scala模板引擎和会话

更新!

这是我工作的代码

请参阅下面我的代码:

 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功能的用户名并保存在我的数据库?

回答

1

如果用户名在会话中(如在其他函数中使用withUser会出现的那样),那么您无需执行任何操作,它将随每个请求自动传递。

虽然你需要做的是恢复它。看起来你可以将Action换成withUser再打电话就行了。

+0

什么是语法html文件?目前我所拥有的是不正确的。 – DrWolfe

+1

您不需要与视图中的会话相关的任何内容,只有在控制器中。您已经将用户名称作为参数传递,如果您想要例如这是您所需要的全部内容显示给他们。 – Philippe

+0

好吧,我会再次测试时,我有机会,很尊重。 – DrWolfe