<li class="aphotos">
<img alt="Assets" src="/assets/">
</li>
- @photos.each do |photo|
%li.aphotos
= image_tag(photo.image_url)
并没有上传过任何照片,但是就是显示有一张这样的照片. 请问是什么原因?该怎么解决?感谢
这个之前是没有问题的,但是昨天把 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
之后,就不知道为会在新建相册的时候,出现这张没有来源的照片了。。。 求解答...
@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:
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)
def show
@album = @user.albums.find(params[:id])
@photos = @album.photos
@photo = @album.photos.build
end
#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
@Rei 嗯。。。懂了,参照<Rails 3 in Action>
,用 merge! 代替原来的一堆代码,然后多手把@photo = Photo.new
改为了@photo = @album.photos.build
.
现在已经恢复正常了,感谢! :)