2014-02-06 84 views
0

對象我有一個域類是這樣的:Grails的處理中選擇

class City { 
String name; 
Country country; 

} 

在創建視圖我希望把國家作爲一個選擇框是這樣的:

<g:select id="country" optionKey="id" optionValue="countryName" 
      from="${Country.list()}" name="country" > 
    </g:select> 

什麼是最好的處理提交方式,以便國家發送作爲對象 (目前我收到一條消息:

Failed to convert property value of type java.lang.String to required type com.example.Country for property country; 

nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.example.Country] for property country: 

no matching editors or conversion strategy found 

注意:我發現這個匹配的問題:Grails select not returning the object but a string但該解決方案沒有解決我的問題。

謝謝。

+0

沒有看到你的控制器代碼唯一的建議是改變select元素的名稱爲「country.id」。這應該通過該id自動綁定到country對象的新實例(取決於您的Grails版本)。 –

+0

感謝Joshua,country.id的工作版本是什麼?我目前正在使用2.2.4 –

+1

我似乎還記得2.3有這個功能,但是如果你在1.3.x的任何版本中使用命令對象,這個綁定也會起作用。同樣,這取決於您的控制器代碼。 –

回答

0

這是因爲Grails控制器不明白如何處理country請求參數的數值。因此,在你的控制器,你應該寫:

def country = Country.get(params.getLong('country')) 
def city = new City(name: params.name, country: country)