1

私は Grails 2.0.4 を使用していますが、次のエラーが表示されます。

メソッドの署名なし: com.example.User.addToDefaultStorePricingProfiles() は、引数の型に適用されます: (com.example.PricingProfile) 値: bindData() 行の [com.example.PricingProfile : 5]。既に保持されている PricingProfiles を追加する前に、最初にユーザーとストアを保存する必要がありますか? または、これを行うためのより良い方法はありますか?

モデル

class User {

    transient springSecurityService

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    Store defaultStore

    Date dateCreated
    Date lastUpdated

    static hasMany = [orders: Order]
}

class Store {

  String storeNumber
  String name
  PricingProfile defaultPricingProfile

  static belongsTo = [retailer: Retailer]

  static hasMany = [pricingProfiles: PricingProfile]
}

class PricingProfile {

  String name

  static belongsTo = [retailer: Retailer]
}

ストアの価格プロファイルのビューで複数選択を使用しています

<g:select from="${retailer.pricingProfiles}" name="defaultStore.pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />

ストアコントローラー

def save() {
    Retailer retailer = Retailer.get(params.retailer)
    User user = new User()
    user.defaultStore = new Store()

    bindData(user, params)

    user.validate()
    user.defaultStore.validate()

    if (user.hasErrors() || user.defaultStore.hasErrors()) {
        log.error("Error saving store: ${user.errors.fieldErrors} ${user.defaultStore.errors.fieldErrors}")
        flash.storeError = "Please correct the errors below"
        render(view: 'create')
    } else {
        retailer.addToStores(user.defaultStore)
        retailer.addToUsers(user)
        retailer.save(failOnError: true, flush: true)

        flash.confirm = "Store ${user.defaultStore.storeNumber} successfully added"
        redirect (action: 'list', params: [retailer: retailer.id])
    }
}

パラメータ:

defaultStore.pricingProfiles: 2
defaultStore.pricingProfiles: 3
defaultStore.pricingProfiles: 4
defaultStore.defaultPricingProfile.id: 2
retailer: 2
submitStore: Save
defaultStore.storeNumber: 888
username: wert
password: wert
defaultStore.name: Fake Store
4

2 に答える 2

0

私の解決策は、pricingProfiles を手動で追加することでした。

<g:select from="${retailer.pricingProfiles}" name="pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />

次に、コントローラーで、

params.pricingProfiles.each {
    PricingProfile pricingProfile = PricingProfile.get(it)
    user.defaultStore.addToPricingProfiles(pricingProfile)
}
于 2012-11-14T15:49:09.853 に答える
0

Storeすべてをにバインドしようとする前に、最初にUser.defaultStoreプロパティの を作成する必要があるようですUser

Grails はそれを自動的に行うことができないようです。Storeと が異なる名前のプロパティを持っていることを考えるとUser、次のことができます。

  1. それを持つパラメーターから を削除しdefaultStore.ます。
  2. 次のようにオブジェクトを作成します。

    Store defaultStore = new Store(params)
    User user = new User(params)
    user.defaultStore = defaultStore
    
于 2012-10-31T19:39:43.167 に答える