2012-07-23 225 views
0

我嘗試創建與Photos有關聯的模型ItemsVenues的多態關聯。我最初使用Rails Best Practices - How do you design your model for multiple upload?,但有一些錯誤,這裏是到目前爲止我的代碼:Ruby on Rails中的多態關聯

class Item < ActiveRecord::Base 
    has_many :photos, :as => :assetable, :class_name => "Item::Photo", :dependent => :destroy 
    accepts_nested_attributes_for :photos 

class Venue < ActiveRecord::Base 
    has_many :photos, :as => :assetable, :class_name => "Photo", :dependent => :destroy 
    accepts_nested_attributes_for :photos 

class Photo < ActiveRecord::Base 
    belongs_to :asset 
end 

class Asset < ActiveRecord::Base 
    has_many :photos 
    belongs_to :assetable, :polymorphic => true 
    delegate :url, :to => :attachment 
end 

class Venue::Photo < Asset 
    has_attached_file :attachment, 
    :styles => { 
     :large => "640x480", 
     :medium => "300x300", 
     :thumb => "100x100" 
    }, 
    :path => "/:style/:id/:filename" 
end 

class Item::Photo < Asset 
    has_attached_file :attachment, 
    :styles => { 
     :large => "640x480", 
     :medium => "300xa300", 
     :thumb => "100x100" 
    }, 
    :path => "/:style/:id/:filename" 

class VenuesController < ApplicationController 

# GET /venues 
# GET /venues.json 
def index 
    #@venues = Venue.all 
    params[:page] ||= 1 
    @venues = Venue.paginate(:page => params[:page]) 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @venues } 
    end 
end 

    # GET /venues/1 
    # GET /venues/1.json 
    def show 
    @venue = Venue.find(params[:id]) 

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

    # GET /venues/new 
    # GET /venues/new.json 
    def new 
    @venue = Venue.new 
    5.times do 
     @venue.assets.build 
    end 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @venue } 
    end 
    end 

    # GET /venues/1/edit 
    def edit 
    @venue = Venue.find(params[:id]) 
    5.times { @venue.assets.build } 
    end 

    # POST /venues 
    # POST /venues.json 
    def create 
    @venue = Venue.new(params[:venue]) 
    #@venue.tag_list ="asian, chinese" 

    respond_to do |format| 
     if @venue.save 
     format.html { redirect_to @venue, notice: 'Venue was successfully created.' } 
     format.json { render json: @venue, status: :created, location: @venue } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @venue.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /venues/1 
    # PUT /venues/1.json 
    def update 
    @venue = Venue.find(params[:id]) 

    respond_to do |format| 
     if @venue.update_attributes(params[:venue]) 
     format.html { redirect_to @venue, notice: 'Venue was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @venue.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /venues/1 
    # DELETE /venues/1.json 
    def destroy 
    @venue = Venue.find(params[:id]) 
    @venue.destroy 

    respond_to do |format| 
     format.html { redirect_to venues_url } 
     format.json { head :no_content } 
    end 
    end 
end 

錯誤消息我收到錯誤消息「未知屬性:assetable_id」當我嘗試編輯一個地點,「未定義的方法`當我嘗試編輯一個項目時,照片爲「Nil:NilClass」

+0

錯誤說的是什麼? – 2012-07-23 13:37:17

+0

@TerencePonce我已經刪除了照片模型,我只是使用資產模型,所以現在我只是在嘗試編輯場地時收到錯誤消息「未初始化的常量場地::照片」 – 2012-07-23 23:22:07

+0

我的意思是說,在你的問題中包含堆棧跟蹤?如果我們沒有堆棧跟蹤繼續,確定原因很難。 – 2012-07-24 02:26:25

回答

0

您是否像設置指南中那樣設置了數據庫?

你爲什麼定義模型photohas_many :photos

如果您的照片樣式相同,則不需要STI。

+0

數據庫如指南中所述。我將刪除模型和has_many:來自Assets模型的照片關聯 – 2012-07-23 22:53:18

+0

您不需要模型照片,因此刪除它並使用資產模型 – 2012-07-23 22:58:27

+0

我刪除了模型照片,現在正在使用模型資源。我得到一個錯誤信息「未初始化的常量地點::照片」時,我嘗試編輯會場 – 2012-07-23 23:09:49