【发布时间】:2012-07-23 13:34:03
【问题描述】:
我的尝试是为与Photos 关联的模型Items 和Venues 创建多态关联。我最初使用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:intimes' app/controllers/venues_controller.rb:45:in `edit' -
您如何编辑您的问题以包括场地控制器?
标签: ruby-on-rails ruby polymorphic-associations polymorphism