我想在導軌中創建一個圖片庫。我製作了一個設置,允許您創建一個相冊並上傳照片。不過,我非常關注如何讓用戶將其中一個現有圖像設置爲圖像索引視圖中的相冊封面。
任何人有一些想法?我發現如果我使用單選按鈕,我無法確定如何確定哪個圖像是由ajax選擇的。我也不知道我會如何強制將一張圖像設置爲專輯封面。導軌中的相冊
這裏是我的設置:
控制器
class Admin::AlbumsController < ApplicationController
respond_to :html, :json
def index
@albums = Album.all
end
def new
@album = Album.new
end
def create
@album = Album.new(params[:album])
if @album.save
flash[:notice] = "Successfully created album!"
redirect_to [:admin, :albums]
else
render "new"
end
end
def edit
@album = Album.find(params[:id])
end
def show
@album = Album.find(params[:id])
end
def update
@album = Album.find(params[:id])
@album.update_attributes(params[:album])
if @album.update_attributes(params[:album])
respond_with @album
flash[:notice] = "Successfully updated Album"
else
render "edit"
end
end
def destroy
@album = Album.find(params[:id])
@album.destroy
@id = @album.id
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/picture/#{@id}", :force => true)
respond_to do |format|
format.js { render :layout => false }
end
redirect_to admin_albums_path
end
def random_image
@image_files = %w(.jpg .gif .png)
@files ||= Dir.entries(
"#{RAILS_ROOT}/public/uploads").delete_if { |x|
[email protected]_files.index(x[-4,4])
}
file = @files[rand(@files.length)];
@files.delete file
return "/images/logos/#{file}"
end
def ajaxUpdate
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:albumcover])
if @image.update_attributes(params[:image])
flash[:notice] = "Successfully updated Image"
else
render "edit"
end
end
end
class Admin::ImagesController < ApplicationController
respond_to :html, :json
#before_filter :split_hash, :only => [ :create, :update ]
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
end
def create
params[:image][:source].each do |image|
@album = Album.find(params[:album_id])
@params = {}
@params['source'] = image
@image = @album.images.create(@params)
end
if @image.save
if params[:image][:source].size > 1
flash[:notice] = "Successfully added images!"
else
flash[:notice] = "Successfully added image!"
end
redirect_to [:admin, @album, :images]
else
render "new"
flash[:notice] = "Did not successfully add image :("
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])
redirect_to [:admin, @album, :images]
flash[:notice] = "Successfully updated Image"
else
render "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
def ajaxUpdate
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:albumcover])
if @image.update_attributes(params[:image])
flash[:notice] = "Successfully updated Image"
else
render "edit"
end
end
# def split_hash
# @album = Album.find(params[:album_id])
# @image = @album.images
# array_of_pictures = params[:image][:picture]
# array_of_pictures.each do |pic|
# size = array_of_pictures.size.to_i
# size.times {@image.build(params[:image], :picture => pic)}
# @image.save
# end
# end
end
模式
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, :source, :album_id, :albumcover, :image, :image_id
belongs_to :album
accepts_nested_attributes_for :album
mount_uploader :source, PictureUploader
end
查看
<% content_for :head do %>
<%= stylesheet_link_tag 'admin/images' %>
<%= javascript_include_tag "admin.js" %>
<% end %>
<% content_for :menu do %>
<li class="menu_item"><%=link_to "New Album", :controller => "albums", :action => "new" %></li>
<li class="menu_item"><%= link_to "Add Images", {:controller => "images", :action => "new"}, :class => "highlight_menu"%> </li>
<% end %>
<%= link_to "< Back", admin_albums_path, :id => "return_link" %> </br>
<h1 class="section-title"> <strong style="font-weight: 600;"><%=best_in_place [:admin,@album], :title, :ok_button => :confirm %></strong></h1>
<h4 class="album-desc"><%= best_in_place [:admin,@album], :description, :type => :textarea, :ok_button => :confirm%></h4>
<%= form_tag admin_album_images_path(@album) do %>
<% if [email protected]? %>
<% @images.each do |image| %>
<div class="item">
<div class="image-box">
<div class="source">
<%= image_tag image.source %>
</div>
</div>
<div class="info">
<div class="item-links">
<%= link_to "Edit", edit_admin_album_image_path(@album, image.id), :id => "edit"%>
<%= link_to "Delete",
admin_album_image_path(@album, image.id),
:class => "item-link delete-image",
:method => :delete,
:remote => true,
:confirm => "Are you sure?" %>
</div>
</div>
</div>
<% end %>
<% else %>
<p class="alert">No images in this album</p>
<% end %>
<% end %>
回答!
* albums_controller *
def albumCoverSet
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:albumcover])
if @image.update_attributes(params[:image])
flash[:notice] = "Successfully updated Image"
else
render "edit"
end
end
*相冊查看*
<div class="image">
<%= image_tag album.images.find(album.albumcover_id).source, :class => "image" %>
</div>
模型
class Album < ActiveRecord::Base
attr_accessible :title, :description, :album_id, :albumcover_id
has_many :images, :dependent => :destroy
has_one :albumcover, :class_name => "Image"
validates :title, :description, :presence => true
end
你能詳細說明爲什麼單選按鈕不起作用嗎?您應該能夠爲客戶端的每個圖像提供一個id。然後選擇一個單選按鈕會發出一條消息,說明帶有該ID的圖像應該是封面。你也可以在圖像上點擊(或者甚至彈出菜單)來實現。 – bchurchill 2013-04-25 23:23:06