如何在视图中显示上传的照片

时间:2014-10-17 16:01:20

标签: ruby-on-rails carrierwave

我正在使用carrierwave gem,我正在尝试在控制器中设置以使用视图中的框版本显示已上传的所有照片(在照片表内)。我似乎只能显示所选用户的照片,但我想显示所有上传内容。

照片控制器:

  def new 
    @photo = Photo.new
  end

  def create
    @photo = Photo.create(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 :back
  end

  def avatar
    if current_user.update_attribute(:avatar_id, params[:id])
      flash[:notice] = "Successfully made Avatar."
        else
          flash[:notice] = "Avatar failed"
        end
        redirect_to :back, notice: "Set as avatar!"
      end
end

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,我认为你的控制器需要一个索引方法

def index
  @photos = Photo.all
end

在您看来,如果您想要显示所有照片,那么您必须循环

- @photos.each do |photo|
   =image_tag photo.content_url

Carrierwave Documentation