【问题标题】:Polymorphic Association in Ruby on RailsRuby on Rails 中的多态关联
【发布时间】:2012-07-23 13:34:03
【问题描述】:

我的尝试是为与Photos 关联的模型ItemsVenues 创建多态关联。我最初使用Rails Best Practices - How do you design your model for multiple upload?,但有一些错误,这是我目前的代码:

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'”

【问题讨论】:

  • 错误说明了什么?
  • @TerencePonce 我删除了照片模型,我只是使用资产模型,所以现在我在尝试编辑场地时收到错误消息“未初始化的常量 Venue::Photo”跨度>
  • 我的意思是你能在你的问题中包含堆栈跟踪吗?如果我们没有要继续的堆栈跟踪,就很难确定原因。
  • 这是应用程序跟踪 app/controllers/venues_controller.rb:45:in block in edit' app/controllers/venues_controller.rb:45:in times' app/controllers/venues_controller.rb:45:in `edit'
  • 您如何编辑您的问题以包括场地控制器?

标签: ruby-on-rails ruby polymorphic-associations polymorphism


【解决方案1】:

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

为什么要定义模型photohas_many :photos

如果您的照片样式相同,则不需要 STI。

【讨论】:

  • 数据库如指南中所述。我将从 Assets 模型中删除模型和 has_many :photos 关联
  • 你不需要模型照片,所以删除它并使用资产模型
  • 我删除了模型照片,现在正在使用模型资产。当我尝试编辑场地时收到错误消息“未初始化的常量 Venue::Photo”
  • :class_name => "照片" 更改为 :class_name => "地点::照片"
  • 更新后报错如下,“未初始化常量Venue::Venue::Photo”
猜你喜欢
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多