私は2回プレイしたばかりですが、詳細を同じページにリダイレクトするときにcRUDアプリケーションを作成しようとしましたが、別のページ にし、私は私のコントローラとモデルとの同梱もログインページは、プレイ中の別のページにリダイレクトする必要があります。2フレームワークでスカラを使用する
package controllers
import play.api._
import play.api.mvc._
import models.Contact
import play.api.Play.current
import play.api.data.Forms._
import play.api.data._
import play.api.Play.current
import javax.inject.Inject
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi
import views._
class Contacts @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def index = Action {
Ok(views.html.index(Contact.all, Contact.form))
}
def create = Action { implicit request =>
Contact.form.bindFromRequest.fold(
errors => BadRequest(views.html.index(Contact.all, errors)),
contact => {
Contact.create(contact)
Redirect(routes.Contacts.index())
}
)
}
def edit(id: Long) = Action {
Contact.get(id).map { contact =>
Ok(views.html.edit(id, Contact.form.fill(contact)))
} getOrElse {
Redirect(routes.Contacts.index())
}
}
def update(id: Long) = Action { implicit request =>
Contact.form.bindFromRequest.fold(
errors => BadRequest(views.html.edit(id, errors)),
contact => {
Contact.update(id, contact)
Redirect(routes.Contacts.index())
}
)
}
def delete(id: Long) = Action {
Contact.delete(id)
Redirect(routes.Contacts.index())
}
}
の下で見て、私のモデルクラス:
package models
import java.lang
import anorm._
import play.api.db.DB
import play.api.Play.current
import play.api.data._
import play.api.data.Forms._
import play.api.i18n.Messages.Implicits._
case class Contact(id: Long, name: String, emailAddress: String,age:Int,phone:Int)
object Contact {
def all = {
DB.withConnection { implicit connection =>
val contacts = SQL("SELECT * FROM contacts")().map { row =>
Contact(
id = row[Long]("id"),
name = row[String]("name"),
emailAddress = row[String]("emailAddress"),
phone=row[Int]("phone"),
age=row[Int]("age")
)
}
contacts.toList
}
}
def create(contact: Contact) {
DB.withConnection { implicit connection =>
SQL("INSERT INTO contacts(name, emailAddress ,phone , age) VALUES({name},{emailAddress},{phone},{age})").on(
"name" -> contact.name,
"emailAddress" -> contact.emailAddress,
"phone"->contact.phone,
"age"->contact.age
).execute()
}
}
def get(id: Long) = {
DB.withConnection { implicit connection =>
SQL("SELECT * FROM contacts WHERE id={id}").on("id" -> id)().headOption.map { row =>
Contact(
id = row[Long]("id"),
name = row[String]("name"),
emailAddress = row[String]("emailAddress"),
phone=row[Int]("phone"),
age=row[Int]("age")
)
}
}
}
def show(contact: Contact) {
DB.withConnection { implicit connection =>
SQL("UPDATE contacts SET name={name}, emailAddress={emailAddress},phone={phone},age={age} WHERE id={id}").on(
"name" -> contact.name,
"emailAddress" -> contact.emailAddress,
"phone"->contact.phone,
"age"->contact.age
).execute()
}
}
def update(id: Long, contact: Contact) {
DB.withConnection { implicit connection =>
SQL("UPDATE contacts SET name={name}, emailAddress={emailAddress},phone={phone},age={age} WHERE id={id}").on(
"id" -> id,
"name" -> contact.name,
"emailAddress" -> contact.emailAddress,
"phone"->contact.phone,
"age"->contact.age
).executeUpdate()
}
}
def delete(id: Long) {
DB.withConnection { implicit connection =>
SQL("DELETE FROM contacts WHERE id={id}").on("id" -> id).execute()
}
}
val form = Form(
mapping(
"id" -> ignored(0L),
"name" -> nonEmptyText,
"emailAddress" -> email,
"phone"->number,
"age"->number
)(Contact.apply)(Contact.unapply)
)
}
と私の意見
@(id: Long,contacts: List[models.Contact], form: Form[models.Contact],contact:Form[models.Contact])(implicit messages: Messages)
@layout {
<h2>Update Contact</h2>
@helper.form(action = routes.Contacts.update(id)) {
@helper.inputText(contact("name"), '_label -> "Name")
@helper.inputText(contact("phone"), '_label -> "phone")
@helper.inputText(contact("age"), '_label -> "age")
@helper.inputText(contact("emailAddress"), '_label -> "Email Address")
<input type="submit" value="Update Contact" />
<div style="width: 100%;
background-color: #4a37ad">
<table>
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>phone</th>
<th>age</th>
</tr>
</thead>
<tbody>
@for(contact <- contacts) {
<tr>
<td>@contact.name</td>
<td>@contact.emailAddress</td>
<td>@contact.phone</td>
<td>@contact.age</td>
<td>
<a href='@routes.Contacts.edit(contact.id)'>Edit</a>
</td>
<td>
@helper.form(action = routes.Contacts.delete(contact.id)) {
<input type="submit" value="Delete!" />
}
</td>
</tr>
}
</tbody>
</table>
</div>
}
}
どのページあなたはリダイレクトしたい行うには?私はあなたの質問を得るか分からない。 – thwiegan
私が提出を押すと、すべての詳細が保存され、同じページに詳細が表示されますが、別のページに実装したいのですがどうすればよいか分かりません –
'Redirect(routes.Contacts.index())'新しいビューを実装し、インデックスビューの代わりにこのビューを呼び出すコントローラメソッドを作成します。または私は何かを逃していますか? – thwiegan