我嘗試創建與Photos
有關聯的模型Items
和Venues
的多態關聯。我最初使用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」
錯誤說的是什麼? – 2012-07-23 13:37:17
@TerencePonce我已經刪除了照片模型,我只是使用資產模型,所以現在我只是在嘗試編輯場地時收到錯誤消息「未初始化的常量場地::照片」 – 2012-07-23 23:22:07
我的意思是說,在你的問題中包含堆棧跟蹤?如果我們沒有堆棧跟蹤繼續,確定原因很難。 – 2012-07-24 02:26:25