我有一个正在构建的应用程序,其中包含带有图像的相册。我希望图像的 id 在添加到相册时从 1 开始。但是,使用我当前的设置,它们不会为每张专辑重置,而是从一张专辑继续编号到下一张。
楷模
class Album < ActiveRecord::Base
attr_accessible :title, :description, :album_id
has_many :images, :dependent => :destroy
validates :title, :description, :presence => true
end
class Image < ActiveRecord::Base
attr_accessible :title, :description, :picture, :image_id, :album_id, :albumcover
belongs_to :album
accepts_nested_attributes_for :album
mount_uploader :picture, PictureUploader
end
图像控制器
class Admin::ImagesController < ApplicationController
respond_to :html, :json
def index
@album = Album.find(params[:album_id])
@images = @album.images.all
end
def new
@album = Album.find(params[:album_id])
@image = @album.images.new(params[:id])
end
def create
@album = Album.find(params[:album_id])
@image = @album.images.new(params[:image])
if @image.save
flash[:notice] = "Successfully added image!"
redirect_to [:admin, @album, :images]
else
render :action => 'new'
end
end
def show
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:id])
end
def edit
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:id])
end
def update
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:id])
if @image.update_attributes(params[:image])
flash[:notice] = "Successfully updated Image"
redirect_to [:admin, @album, :images]
else
render :action => "edit"
end
end
def destroy
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:id])
@image.destroy
@albumid = @album.id
@id = @image.id
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/picture/#{@albumid}/#{@id}", :force => true)
redirect_to admin_album_images_path(@album)
end
end
如何使每张专辑中的编号从 1 开始?