2012-09-04 2 views
0

내 웹 응용 프로그램의 사용자가 게시물을 작성하고 싶습니다. 게시물에는 그가 게시했음을 나타내는 사용자 이름이 포함됩니다. 어떻게하면 좋을까요?재생! 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의 사용에서 나타나는 것처럼) 사용자는 아무 것도하지 않고 요청할 때마다 자동으로 전달됩니다.

그래야 복구 할 수 있습니다. ActionwithUser으로 다시 호출하면 작동하는 것으로 보입니다.

+0

구문 html 파일은 무엇입니까? 현재 내가 가지고있는 것은 정확하지 않습니다. – DrWolfe

+1

보기에서만 세션과 관련된 사항은 컨트롤러에서 필요하지 않습니다. 이미 사용자 이름을 인수로 전달하고 있습니다. 예를 들어 원하는 경우 사용자 이름 만 있으면됩니다. 그것을 그들에게 보여라. – Philippe

+0

좋아, 나는 많은 기회를 가질 때 다시 시험 할 것이다. – DrWolfe