2012-07-06 21 views
0

我正在使用回形針和導軌。我遵循本教程: Multiple File Uploads with Paperclip & Rails 3 (Screencast)如何在一對多關係中鏈接到頁面?

我有一個畫廊模型,有很多galleryphotos。

而是鏈接到在我看來,原始圖像的,我創建了光象表演方法,但我得到了以下錯誤:

undefined method `galleryphoto_path' for #<#<Class:0x007f40a0000c20>:0x00000005385d60> 

這裏是我的代碼:

<% for asset in gallery.galleryphotos %> 
    <%= link_to image_tag(asset.photo.url(:thumb)), url_for(asset) %> 
<% end %> 

這裏是我的galleryphoto控制器:

class GalleryphotosController < ApplicationController 
    load_and_authorize_resource 

    def show 
    @gallery = Gallery.find(params[:gallery_id]) 
    @galleyphoto = @gallery.galleyphotos.find(params[:id]) 


    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @gallery } 
    end 
    end 
end 

這裏是我的這些路線:

resources :galleries do 
    resources :galleryphotos 
    end 

任何想法如何解決這個問題?

+0

So處於PhotoImagesController Show方法? (但模型叫做galleryphoto)?想確保我有這個權利。 – 2012-07-06 01:32:07

+0

@JesseWolgamott - 是的。我的控制器是GalleyphotosController – bigdaveyl 2012-07-06 01:46:49

+0

這是代表你的routes.rb文件? – Rabbott 2012-07-06 03:22:46

回答

2

url_for幫助程序找不到Galleryphoto模型的路線,因爲它取決於Gallery模型。它在路由中被聲明爲嵌套資源,因此Galleryphoto在url中沒有Gallery就不存在。

要解決這個問題請執行下列操作的:

<%= link_to image_tag(asset.photo.url(:thumb)), url_for(gallery, asset) %> 

<%= link_to image_tag(asset.photo.url(:thumb)), gallery_galleryphoto_path(gallery, asset) %> 
+0

這使我更接近。鏈接顯示在我的畫廊頁面上。第二種解決方案有效。但是,我現在在控制器中出現此錯誤:GalleryphotosController中的NoMethodError#show 未定義的方法'galleyphotos'for# bigdaveyl 2012-07-06 13:13:02

+0

@bigdaveyl這是一個無關的錯誤。這是因爲你的'Gallery'模型沒有':has_many:galleryphotos'或者它的命名方式不同。 – Draiken 2012-07-06 15:46:45

+0

不是這樣。我會發表另一個問題 – bigdaveyl 2012-07-07 16:24:59

相關問題