2016-08-08 16 views
1

テーブルがあります:製品とショップ(grails generate-allを使用してコントローラを生成します)。新しい製品を作成する際にデータベースを選択したショップ。 私はそれがクラスのg:grailsのデータベース選択保存選択

プロパティ[ショップ] [クラスtr.com.nur.storeapp.Product]は

nullにすることはできませんと言うボタンの作成]をクリックしたときに、私は

<g:form controller="product" action="save" role="form"> 
    <div class="form-horizontal" role="form"> 
     <div class="form-group"> 
      <label class="col-lg-3 control-label">Product Name:</label> 
      <div class="col-lg-8"> 
       <g:textField name="productName" class="form-control" value="${product.productName}"/> 
      </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-lg-3 control-label">Product Barcode</label> 
      <div class="col-lg-8"> 
       <g:textField name="date expired" class="form-control" value="${product.productBarcode}"></g:textField> 
      </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-lg-3 control-label">Product Shop</label> 
      <g:select class="selectpicker" name="products.id" from="${tr.com.netiket.lkkstoreapp.Shop.list()}" value="shop?.products.id" optionValue="shopName"></g:select> 
     </div> 
</g:form> 

を使用してすべての値をリストされています

+0

私は私の問題を解決しました。私は他の人を助けるためにここに書きました。私がやったことは、オプションキーをgに追加することです。

答えて

0

このビットは、右を見ていない:

<g:select class="selectpicker" name="products.id" from="${tr.com.netiket.lkkstoreapp.Shop.list()}" value="shop?.products.id" optionValue="shopName"></g:select>

名は、ショップのIDである必要があり、存在する場合値は、製品の店舗IDでなければなりません:これは私の保存方法である

<g:select class="selectpicker" name="shop.id" from="${tr.com.netiket.lkkstoreapp.Shop.list()}" value="${product?.shop?.id}" optionValue="shopName"></g:select>

+0

答えに@Greggに感謝します。しかし、これは言う、非nullプロパティは一時的な値を参照する - 一時的なインスタンスは現在の操作の前に保存する必要がありますあなたは何か考えています –

+0

あなたはあなたの保存メソッドを提示する必要があります。私はそれが関連しているのか、この問題を乗り越えて別の問題を発見したのだろうかと思います。 – Gregg

+0

私は答えとして投稿しました –

0
@Transactional 
def save(Shop shop) { 
    //println "in shop save" 
    def currentUser=(User)springSecurityService.currentUser 
    shop.user=currentUser 
    shop.validate() 
    if (!shop) { 
     //println "I have no shop" 
     transactionStatus.setRollbackOnly() 
     notFound() 
     return 
    } 

    //if (shop.hasErrors()) { 
    if (shop.save(flush:true)) { 
     //println "shop has errors" 
     transactionStatus.setRollbackOnly() 
     respond shop.errors, view:'create' 
     shop.errors.allErrors 
     return 
    } 

    //shop.save flush:true 
    //println "shop has saved" 

    request.withFormat { 
     form multipartForm { 
      flash.message = message(code: 'default.created.message', args: [message(code: 'shop.label', default: 'Shop'), shop.id]) 
      //println "redirecting" 
      redirect shop 
     } 
     '*' { respond shop, [status: CREATED] } 
    } 
} 

。実際には私はここに何も書きませんでした。ドメインを生成する。

+0

私はあなたの答えを変更し、可能な限り単純にしました - 基本的には同じものがたくさんありましたが、printlnsをコメントアウトしました - あなたがコードを書いている間にif文などのprintln文をテストして、すべてが機能していることを確認し、適切な場所に置いてください。 – Vahid

+0

補足として。 SOはユーザー自身の質問に答えても何の問題もありません。これは実際の答えではありません。したがって、それを削除して実際の質問を編集し、それに上記を追加する価値があります。私はあなたの問題を解決したことを願っています。このコメントは、この質問とネガティブなスコアを得ることからあなた自身の評判を参照するかもしれない将来のユーザーのための良いプラクティスに関するものです – Vahid

関連する問題