【发布时间】:2014-01-21 19:34:11
【问题描述】:
我安装了 CarrierWave gem。没有画廊,因为我已将其设置为照片属于用户而不是画廊。
我在照片表中添加了一个avatar 列。其他列是:id、created_at、updated_at、image、name、user_id
问题是如何设置操作,以便用户可以单击“制作个人资料图片”并更改avatar 列?这应该通过单击图像的缩略图版本或覆盖文本来完成。
我的意思的一个例子:
用户 Z 将四张照片上传到他们的个人资料。用户 Z 访问他们的个人资料并选择他们上传的一张照片作为他们的个人资料图片(头像)。
class PhotosController < ApplicationController
def new
@photo = Photo.new
end
def create
@photo = Photo.new(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to :back
else
render :action => 'new'
end
end
def resize(width, height, gravity = 'Center')
manipulate! do |img|
img.combine_options do |cmd|
cmd.resize "#{width}"
if img[:width] < img[:height]
cmd.gravity gravity
cmd.background "rgba(255,255,255,0.0)"
cmd.extent "#{width}x#{height}"
end
end
img = yield(img) if block_given?
img
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(paramas[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to @photo.gallery
else
render :action => 'edit'
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to @photo.gallery
end
def avatar
@photo = Photo.find params[:photo_id]
current_user.default_photo = @photo
redirect_to '/profile'
end
end
查看(在个人资料上显示拇指照片):
<div class="parent-container">
<% @user.photos.each do |photo| %>
<%= link_to image_tag(photo.image_url(:thumb)), photo.image_url%>
<% end %></div></p>
【问题讨论】:
-
我将创建一个新实体:照片 user_id:int album_id:int 用于存储所有用户的所有照片,然后允许每个用户通过添加用户 photo_id:int 将任何照片设置为他们的个人资料中的照片。还限制了“只为恶魔”设置照片的权限。如果你需要,我可以在 Github 中实现它。
标签: javascript css ruby-on-rails carrierwave photos