rails中的专辑

时间:2013-04-25 23:18:46

标签: ruby-on-rails

我想在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 %>

答案!

* 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

1 个答案:

答案 0 :(得分:2)

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

has_one :primary_image, :class_name => 'Image'

在表单中,您可以显示所有album.images并选择一个。单选按钮应该可以正常工作。

提交param的值将设置primary_image_id。