Railsで画像ギャラリーを作成したいと思います。アルバムを作成してそこに写真をアップロードできるセットアップを作成しました。ただし、ユーザーが既存の画像の1つを画像のインデックスビューでアルバムカバーとして設定できるようにする方法に困惑しています。
誰にもアイデアはありますか?ラジオ ボタンを使用すると、どの画像が ajax によって選択されたかを判断する方法がわからないことがわかりました。また、アルバム カバーとして 1 つの画像のみを強制的に設定する方法もわかりません。
これが私のセットアップです:
コントローラ
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|
!@image_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 !@images.blank? %>
<% @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