简体   繁体   English

Ruby on Rails中的多态关联

[英]Polymorphic Association in Ruby on Rails

My attempt is to create a polymorphic association for models Items and Venues which have an association with Photos . 我的尝试是为与Photos关联的模型ItemsVenues创建多态关联。 I initially used Rails Best Practices - How do you design your model for multiple upload? 我最初使用了Rails最佳实践-如何设计用于多次上传的模型? but have a few errors, here is my code so far: 但有一些错误,这是到目前为止的代码:

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

The error message I get the error message "unknown attribute: assetable_id" when I try to edit a venue and "undefined method `photos' for nil:NilClass" when I try to edit an Item 错误消息,当我尝试编辑场所时收到错误消息“未知属性:assetable_id”,而当我尝试编辑项目时收到错误消息“ nil:NilClass的未定义方法”照片”

Did you setup db as it showed in guide? 您是否按照指南所示设置了数据库?

Why did you define model photo and has_many :photos ? 为什么要定义模型photohas_many :photos

If you have same styles for photos you don't need STI. 如果您的照片样式相同,则无需STI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM