【发布时间】:2016-06-28 18:03:14
【问题描述】:
在我的网络应用中,每个相册都有很多照片,每张照片都属于一个相册。我已经完成了关联以及新创建的照片操作,我没有收到错误,但是,当我转到相册显示操作时,新创建的照片没有出现。
查看:
<% @photos.each do |x| %>
<div class="new-photo">
<%= image_tag x.image, :style => "height:200px; width:200px; border-radius:5px;" %>
</div>
<% end %>
相册控制器:
def show
@album = current_user.albums.find(params[:id])
@photos = @album.photos
@interests = current_user.interests
end
照片控制器:
def new
@photo = current_user.photos.new
end
def create
@photo = current_user.photos.new(photo_params)
if @photo.save
redirect_to '/albums'
else
render '/photos/new'
end
end
private
def photo_params
params.require(:photo).permit(:image)
end
照片模特:
class Photo < ActiveRecord::Base
belongs_to :album
belongs_to :user
结束
专辑型号:
class Album < ActiveRecord::Base
belongs_to :user
has_many :photos
结束
【问题讨论】:
-
在您的创建操作中使用
build而不是new。 this 可能会对您有所帮助。 -
还是一样的结果
-
你能把你的型号代码给过去吗?你有任何验证吗?
-
如何上传图片? params.require(:photo).permit(:image)...图像参数的值是多少?
-
您已经仔细检查了数据库以确保您的照片正在保存并保存到正确的相册中?
标签: ruby-on-rails ruby