新手问题 照片展示,Rails 内无故生成一张无内容照片

levan · 2013年05月04日 · 最后由 Levan 回复于 2013年05月05日 · 3758 次阅读
<li class="aphotos">
   <img alt="Assets" src="/assets/"> 
</li>
- @photos.each do |photo|
   %li.aphotos
      = image_tag(photo.image_url)

并没有上传过任何照片,但是就是显示有一张这样的照片. 请问是什么原因?该怎么解决?感谢

这显示图片是没有存在啊! @photos这对象有东西....

@kevinhua ???不懂。。。 @kaka 添加一张图片之后的样子。。。

这个之前是没有问题的,但是昨天把 photos_controller#create 改为:

def create
    @photo = @album.photos.build(params[:photo].merge!(:user => current_user))

    if @photo.save
      redirect_to [@album.user, @album], notice: 'Photo was successfully created.'
    else
      render :new
    end
end

之后,就不知道为会在新建相册的时候,出现这张没有来源的照片了。。。 求解答...

这图片是个占位符,说明有个 img 标签载入图片失败。

@kevinhua 看到 stackfolow 里的讨论大概懂你的意思了,貌似有点深度。。。 @Rei 加多了一个判断

%li.aphotos
    = image_tag(photo.image_url) if photo.image.present?

不会显示那张失效的图片了。不过还是没从根本上解决,我再看看。 谢谢

@Rei 请问怎样才能消除那个呢?就是我在创建 album 的之后,查看新建的 album 就发现里面有这个了。并不知道他是怎么来的。

@Rei 你好,这个是 debug 的结果(我不会用 debug,只知道可以显示一些对象的内容) 这个是我上传一张图片之后的样子,在没有上传图片的时候,就已经有了限免俺哥 attributes 的内容。 也就是你所说的那个占位符的内容。

- !ruby/object:Photo
                   attributes:
                     id: 1
                     user_id: 1
                     album_id: 1
                     name: ''
                     desctiption: 
                     comments_count: 
                     image: 2.jpg
                     created_at: 2013-05-04 16:51:45.000000000 Z
                     updated_at: 2013-05-04 16:51:45.000000000 Z
                 - !ruby/object:Photo
                   attributes:
                     id: 
                     user_id: 
                     album_id: 1
                     name: 
                     desctiption: 
                     comments_count: 
                     image: 
                     created_at: 
                     updated_at: 

#9 楼 @Levan 贴 controller 和 view 的代码。

photos_controller.rb

def create
  @photo = @album.photos.build(params[:photo].merge!(:user => current_user))

  if @photo.save
    redirect_to [@album.user, @album], notice: 'Photo was successfully created.'
  else
    render :new
  end
end

albums/show.html.haml

%ul
    - @photos.each do |photo|
        %li.aphotos
            = image_tag(photo.image_url) if photo.image.present?

= link_to 'new photo',new_album_photo_path(@album)

@Rei

#11 楼 @Levan Controller 完整代码

@Rei

def show
    @album = @user.albums.find(params[:id])
    @photos = @album.photos
    @photo = @album.photos.build
end

@Rei 哦,等等

@Rei

#encoding: utf-8
class AlbumsController < ApplicationController
    before_filter :authenticate_user!, :except => [:index, :show]
    before_filter :get_user
    layout 'album'

    def index
        @albums = @user.albums
    end

    def new
        @album = @user.albums.new
    end

    def create
        @album = @user.albums.build(params[:album].merge!(:user => current_user))

        if @album.save
            redirect_to user_albums_path(@user),notice: "相册创建成功" 
        else
            render :new, notice:"相册创建失败"
        end
    end

    def show
        @album = @user.albums.find(params[:id])
        @photos = @album.photos
        @photo = @album.photos.build
    end

    def destroy
        @album = current_user.albums.find(params[:id])
        @album.destroy
        redirect_to user_albums_path(@user), notice:"已成功删除#{@album.name}相册!"
    end

    def edit
        @album = @user.albums.find(params[:id])
    end

    def update
        @album = @user.albums.find(params[:id])
        if @album.update_attributes(params[:album])
            redirect_to user_albums_path(@user)
        else
            render action: "edit"
        end
    end

    private

    def get_user
        @user = User.find(params[:user_id])
    end
end

#13 楼 @Levan show action 这里 @photo = @album.photos.build 创建了一个 @photo ,虽然还没保存,已经会出现在 @photos 了。@photos 是个延时查询。

@Rei 嗯。。。懂了,参照<Rails 3 in Action>,用 merge! 代替原来的一堆代码,然后多手把@photo = Photo.new改为了@photo = @album.photos.build.

现在已经恢复正常了,感谢! :)

需要 登录 后方可回复, 如果你还没有账号请 注册新账号