"Go Web Development Revel+mgo" The 6th chapter realizes the message board function

Source: Internet
Author: User
Tags button type
This is a creation in Article, where the information may have evolved or changed.

Demo Address http://gblog-revel.herokuapp.com/


1. Implement Message Board

We did a simple design in the previous chapter, pick on the message form has, then .....
First, create a new message.go content under App/models:
Package Modelsimport ("Github.com/revel/revel" "Labix.org/v2/mgo/bson" "Time") type Message struct{email stringqq StringUrl stringcdate time. Timecontent string}func (Message *message) Validate (v *revel. Validation) {v.check (message). Email,revel. Required{},revel. MAXSIZE{50},) v.email (message. Email) V.check (message. Qq,revel. MAXSIZE{20},) V.check (message. Url,revel. MAXSIZE{200},) V.check (message. Content,revel. Required{},revel. Minsize{1},revel. maxsize{1000},)}func (DAO *dao) insertmessage (message *message) error {messcollection: = Dao.session.DB (DbName). C (messagecollection)//set the timemessage. CDate = time. Now (); Err: = Messcollection.insert (message) if err! = Nil {Revel. WARN. Printf ("Unable to save message:%v error%v", Message, Err)}return Err}func (dao *dao) findallmessages () []message{messcol Lection: = Dao.session.DB (DbName). C (messagecollection) Mess: = []message{}query: = Messcollection.find (Bson. m{}). Sort ("-cdate") query. All (&mess) return mess}

Take a look, more similar to our comment.go.
Create a new wmessage.go content under App/controllers:
Package Controllersimport ("Github.com/revel/revel" "Gblog/app/models" "Strings" "FMT") type wmessage struct {app}func ( C wmessage) putup (Message *models. Message) Revel. Result {message. Email = strings. Trimspace (message. Email), message. URL = strings. Trimspace (message. URL); message. Content = strings. Trimspace (message. Content); message. QQ = strings. Trimspace (message. QQ); message. Validate (c.validation) fmt. Println (c.validation) if C.validation.haserrors () {c.validation.keep () C.flashparams () c.flash.error ("Errs:The Email And the content should not being null,or the maxsize of email is 50. ") Return C.redirect (app.message)}dao, err: = models. Newdao () if err! = Nil {c.response.status = 500return c.rendererror (err)}defer DAO. Close () Err = DAO. Insertmessage (Message) if (err!=nil) {c.response.status = 500return c.rendererror (err)}return C.redirect (app.message) }

Well, it's very familiar.
OK, to add the route, add it in Conf/routes:
POST    /putmessage                 wmessage.putup

OK, let's show it.
The Message () method in App.go is modified to:
Func (c App) Message () Revel. Result {DAO, err: = models. Newdao () if err! = Nil {c.response.status = 500return c.rendererror (err)}defer DAO. Close ()//dao: = models. Newdao (c.mongosession) messages: = DAO. Findallmessages () return C.render (Messages)}

The views/app/message.html was last modified to:
{{set.} "title" "Message-gblog"}}{{set. "Mess" "Active"}}{{template "header.html".}}         <div class= "Content" > <div class= "Comment-nav" > <div class= "comment-title" > Message board <span style= "Float:right" title= "Total Messages" >[{{len .messages}}]</span> </div> <div class= "Comment-cell" > <div class= "Comment-inputbox" > <form action= "/putmessage" method= "Pos T "> <ul class=" Comment-input-infor "> {{with $field: = Field" message.                    Email ".}} <li> email <input type= "email" id= "{{$field. Id}} "Name=" {{$field. Name}} "class=" Form-control "required value=" {{if $field. error}}{{$field. error}}{{else}}{{$field.                      Value}}{{end}} "/> </li> {{end}} <li> QQ {{with $field: = Field "MessagE.qq ".}} <input type= "text" id= "{{$field. Id}} "Name=" {{$field. Name}} "class=" Form-control "value=" {{if $field. error}}{{$field. error}}{{else}}{{$field.                      Value}}{{end}} "/> {{end}} </li> <li> Personal Home Page {{with $field: = Field ' message.                      Url ".}} <input type= "text" id= "{{$field. Id}} "Name=" {{$field. Name}} "class=" Form-control "placeholder=" Don ' t with http://"value=" {{if $field. error}}{{$field. error}}{{else}}{{$field.                  Value}}{{end}} "/> {{end}} </li> </ul>                      <div class= "Comment-input-text" > MESSAGE <div> {{with $field: = Field ' message.                      Content ".}} <textarea class= "Form-control ctextarea" id= "{{$field. Id}} "Name=" {{$field. Name}} "Required>{{if $field. Error}}{{$field. error}}{{else}}{{$field.                   value}}{{end}}</textarea> {{end}} </div> </div>            <button type= "Submit" class= "btn btn-success comment-input-text-btn" >SUBMIT</button> </form> </div> </div> {{if. Messages}} {{range $index, $message: =.messages} {{if Mo $index 2}} <div class= "Comment-cell" > <pre ><code><span class= "Func-col or ">func</span><span class=" Func-name "> Usermessage</span><span class=" PLN "> () </ Span><span class= "PLN" >{</span><span class= "pln-w" > </span><span class= "Func-color" >var</span><span class= "Pln-w" > </span><span class= "PLN" >email </span><span class= "Func-type" >string </span><span class= "PLN" >=</span><span class= "PLN" > </span ><span class= "FuNc-str ">" {{$message. Email}} "</span><span class=" PLN ">;</span>{{if $message. Qq}}<span class= "Pln-w" > </span><span class= "func-color" >var</span><span class= "Pln-w" > </span><span class= "PLN" >qq </span><span class= "PLN" >=</span><span class= " Pln-w "> </span><span class=" func-str ">{{$message. Qq}}</span><span class= "PLN" >;</span>{{end}}{{if $message. Url}}<span class= "Pln-w" > </span><span class= "func-color" >var</span><span class= "Pln-w" > </span><span class= "PLN" >url </span><span class= "PLN" >=</span><span class= " Pln-w "> </span><a href=" http://{{$message. URL}} "class=" Func-str "target=" _blank ">http://{{$message. Url}}</a><span class= "PLN" >;</span>{{end}}<span class= "Pln-w" > </span><span class = "PLN" >date </span><span class= "Func-color" >:=</span> <span class= "pln-w" > </span><span class= "func-str" > "{{$message.    Cdate.format "2006-01-02 15:04"} "</span><span class=" PLN ">;</span><span class=" Pln-w "> </span><span class= "Func-color" >var </span><span class= "PLN" >message</span><span class= "Pln-w" > </span><span class= "PLN" >=</span><span class= "pln-w" > </span>< Span class= "Func-str" > "{{$message. Content}} "</span><span class=" PLN ">;</span><span class=" PLN ">}</span><span class = "Pln-w" ></code></pre> </div> {else} <div class= "Comment-cell" > &lt ;p re style= "background: #2d2d2d;" ><code><span class= "Func-color" >func</span><span class= "Func-name" > UserMessage</ Span><span class= "Pln-w" > () </span><span class= "pln-w" >{</span><span class= "Pln-w" > </span><span class= "Func-color" &Gt;var</span><span class= "Pln-w" > </span><span class= "pln-w" >email </span><span class= "Func-type" >string </span><span class= "pln-w" >=</span><span class= "pln-w" > </ Span><span class= "Func-str" > "{{$message. Email}} "</span><span class=" pln-w ">;</span>{{if $message. Qq}}<span class= "Pln-w" > </span><span class= "func-color" >var</span><span class= "Pln-w" > </span><span class= "pln-w" >qq </span><span class= "pln-w" >=</span><span class= "Pln-w" > </span><span class= "func-str" >{{$message. Qq}}</span><span class= "Pln-w" >;</span>{{end}}{{if $message. Url}}<span class= "Pln-w" > </span><span class= "func-color" >var</span><span class= "Pln-w" > </span><span class= "pln-w" >url </span><span class= "Pln-w" >=</span><span class = "Pln-w" > </span><a href= "http:{{$message.} URL}} "class=" Func-str "target=" _blank ">http://{{$message. Url}}</a><span class= "pln-w" >;</span>{{end}}<span class= "pln-w" > </span><span class= "Pln-w" >date </span><span class= "Func-color" >:=</span><span class= "pln-w" > </ Span><span class= "Func-str" > "{{$message.    Cdate.format "2006-01-02 15:04"}} "</span><span class=" pln-w ">;</span><span class=" Pln-w "> </span><span class= "Func-color" >var </span><span class= "Pln-w" >Message</span>< Span class= "pln-w" > </span><span class= "pln-w" >=</span><span class= "pln-w" > </span ><span class= "Func-str" > "{{$message. Content}} "</span><span class=" pln-w ">;</span><span class=" Pln-w ">}</span><span class= "Pln-w" ></code></pre> </div> {{end}} {{end}} {{end}} </div></ Div>{{template "Footer.html" .}} 


Yes, finished? Are you done? Take a look at this piece of code:

{{if Mo $index 2}}

Well, we've added a custom method, remember what the last one did? Open App/init.go added in the Func init method:
Revel. templatefuncs["Mo"] = func (A, b int) bool {return a%b==0}

This time we really finished, look at the effect:



Source Address:Https://github.com/joveth/GBlog

AC qq:158325682

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.