【问题标题】:Adding ProfileImage to Photos controller将 ProfileImage 添加到照片控制器
【发布时间】: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


【解决方案1】:

你有一个User 和一个UserPhotos,其中一个可能是Avatar...

我建议你在User 上建立一个avatar_id,并建立如下关系:

class User < ActiveRecord::Base
  has_many :photos
  belongs_to :avatar, class_name: 'Photo'
end

然后在你的controller,如果你喜欢添加一个新的action,它会是这样的:

class PhotosController < ApplicationController
  ...

  def avatar
    if current_user.update_attribute(:avatar_id, params[:id])
      #set flash success
    else
      #set flash fail
    end
    redirect_to(current_user) # However you plan to handle this...
  end

  ...
end

(您可能想要使用新控制器的原因是为了保持“DRY”)

我假设你会有这样的成员 route

resources :photos do
  member do
    post :avatar
  end
end

还有一个像这样的action

button_to('Set as Avatar', [:avatar, @photo])

然后你像这样在视图中引用它:

@user.avatar.image_url(:thumb)

这应该可以解决问题。

【讨论】:

  • 我同意在 users 表中有一个 avatar_id 是最好的选择。但是在User 模型中有一行belongs_to :avatar 让我感到难过,并提醒我“你拥有的东西最终拥有你”这句话是多么真实...... :)
【解决方案2】:

您可以这样做,以便渲染可用照片的图库并允许用户为他们的 DP/头像/个人资料图片选择一张

class UserAvatarController < ApplicationController

def edit
@gallery = current_user.gallery
end

def update
if params[:photo_id].present?
  current_user.update_attributes avatar_id: params[:photo_id]
else
  flash[:error] = "No photo selected"
  render action: "edit"
  end
 end
end

【讨论】:

  • 似乎没有必要给它一个单独的控制器。我不应该只是能够将avatar 列添加到 Photos 表中,然后在 Photos 控制器中分配一个操作,使所选照片 ID 的列不为 NULL 吗?这就是我的愿景,但对于如此简单的东西,我似乎无法编译它。
猜你喜欢
  • 1970-01-01
  • 2016-10-07
  • 2011-05-12
  • 1970-01-01
  • 2011-07-07
  • 2010-11-18
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多