2

我想在 Rails 中创建一个图片库。我制作了一个设置,允许您创建相册并将照片上传到其中。但是,我对如何允许用户将现有图像之一设置为图像索引视图中的专辑封面感到困惑。
有人有什么想法吗?我发现如果我使用单选按钮,我无法弄清楚如何确定 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|
        !@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 %>

回答!

*专辑控制器*

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
4

1 回答 1

2

您可以在迁移中将“primary_image_id”添加到相册。

has_one :primary_image, :class_name => 'Image'

在您的表单中,您可以显示所有专辑图片并选择一个。单选按钮应该可以正常工作。

提交参数的值将设置primary_image_id。

于 2013-04-25T23:39:33.600 回答