2017-04-17 41 views
-1

我有兩個表:酒店及內容的Grails:從相關表中顯示故障數據

內容域看起來是這樣的,正在生成hotelId:

class Content { 

Hotel hotel 
String imageUrl 

static constraints = { 
    imageUrl nullable:true 
} 

我從上傳圖片到S3酒店的展示頁面到相關的內容表,這是行得通的 - 現在我想將我的內容表中的圖像顯示到相關酒店對象(其中酒店= hotelId的內容ID)的顯示頁面。

我已經在這幾天,這是我的腳是最接近糾正:

HotelController:

def showContent() 
{ 
    def content = Content.findByHotelId(hotel.params.id) 
    render content: content 
} 

酒店的看法:

<g:if test="${content.hotelId}" action="showContent"> 
    <img src = "${content.imageUrl}" /> 
</g:if> 

這代碼將導致以下錯誤: '無法獲取'空對象'上的屬性'hotelId'。

任何幫助將不勝感激。

+0

我只是想知道這個功能:findByHotelId。你確定這是正確的。在我看來,應該只有一個findByHotel() – Torsten

+0

不,我不確定findByHotelId是否正確,但是我剛剛嘗試過使用findByHotel,並且遇到同樣的錯誤。這絕對是小東西一樣,雖然這是不對的 –

+0

酒店賓館= Hotel.findById(params.id) 清晰內容= Content.findByHotel(酒店) 我彪這樣 – Torsten

回答

0

堅持到標準腳手架表演動作HotelController

def show(Hotel hotel) { 
     respond hotel 
} 

然後在酒店show.gsp每個內容:

<g:each in="${hotel.contents}" var="content"> 
    <img src = "${content.featuredImageUrl}" /> 
</g:each> 

我想你想包裝上面的每一箇中以某種方式,它或風格。

+0

謝謝你,但是這仍然是返回相同錯誤和以前一樣,'無法獲得屬性'hotelId'爲空對象' –

+0

您嘗試呈現的內容域是否有關聯的酒店?嘗試'' –

+0

是的。 ID爲1的酒店與HotelId爲1的內容相關聯,等等。添加'?'加載的頁面沒有拋出錯誤,但沒有包含在頁面上 –