0

我的尝试是为模型创建一个多态关联,Items并且VenuesPhotos. 我最初使用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 的未定义方法 `photos'”

4

1 回答 1

0

您是否按照指南中的说明设置了 db?

你为什么定义模型photohas_many :photos

如果您的照片风格相同,则不需要 STI。

于 2012-07-23T14:46:52.307 回答