Ruby on rails 如何从数据库返回对象,但仍然需要渲染错误,rails
我有一个微型模型,一个画廊模型和一个图片模型,这些图片要么属于微型模型,要么属于画廊模型。我有一个micropost表单,它Ruby on rails 如何从数据库返回对象,但仍然需要渲染错误,rails,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个微型模型,一个画廊模型和一个图片模型,这些图片要么属于微型模型,要么属于画廊模型。我有一个micropost表单,它接受图片的嵌套属性,并在模型中设置关联,控制器micropost创建动作和强参数如下所示 Micropost_控制器 def create @micropost = current_user.microposts.create(micropost_params) @pictures = @micropost.pictures @pictures.each do
接受图片的嵌套属性,并在模型中设置关联,控制器micropost创建动作和强参数如下所示
Micropost_控制器
def create
@micropost = current_user.microposts.create(micropost_params)
@pictures = @micropost.pictures
@pictures.each do |pic|
pic.update!(gallery_id: params[:gallery_id])
end
...
render 'static_pages/home'
end
def micropost_params
params.require(:micropost).permit(:content, pictures_attributes: [:id, :gal_refs_id, :picture, :_destroy]))
end
画廊是预设的,所以我有它的id,它在一个隐藏的字段中发送,并在microposts控制器中检索,如图所示
问题是我需要返回@micropost
对象,以便更新:gallery\u id
,因此我不能使用@micropost.save
,因为它只返回true
或false
,如果出现错误,我需要重新呈现表单
此时,使用create-it将以静默方式失败并呈现模板
我怎样才能克服这个问题,或者有更好的方法吗
提前谢谢 型号:
class Micropost
has_many :pictures, as: :imageable, inverse_of: :imageable
end
class Picture
belongs_to :imageable, polymorphic: true
end
class Gallery
has_many :pictures, as: :imageable, inverse_of: :imageable
end
# Note: inverse_of is important here! It allows you to instantiate the
# Picture object without having a Picture object yet.
控制器:
def create
@micropost = micropost.new(micropost_params)
@pictures.each do |picture|
@micropost.pictures.new({
picture: picture,
gallery_id: params[:gallery_id]
})
end
# Note: Pictures will not be saved or persisted in the database until
# .save is called.
# Note: If your Picture object is invalid, Micropost save will also fail
if @micropost.save
# Saved, do something here with the success response
render 'static_pages/home'
else
# Errors
@micropost.errors
end
end
图片验证:
这是我的生产应用程序中的代码,请根据您的需要进行调整
steam_game_details['images'].each do |image|
image_file_name = image.split('/').last.split('?').first
local_picture = picture.where({
picture_file_name: image_file_name
}).first_or_initialize
# Check if the image is nil and whether or not it actually exists on AWS S3
#
if local_picture.picture.nil? || !local_picture.picture.exists?
local_picture.assign_attributes({
picture: open(URI.parse(image)),
picture_file_name: image_file_name # Overwrite the name generated by Paperclip
})
end
end
尝试使用下面的代码。如果@micropost没有错误,它将创建@micropost,您可以更新其关联对象
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
# if the object has no errors
@pictures = @micropost.pictures
@pictures.each do |pic|
pic.update!(gallery_id: params[:gallery_id])
end
...
render 'static_pages/home'
else
# show Errors
@micropost.errors
end
end
def micropost_params
params.require(:micropost).permit(:content, pictures_attributes: [:id, :gal_refs_id, :picture, :_destroy]))
end
这里的@pictures
是什么?您是否遗漏了@pictures=@microspost.pictures
?是的,对不起<代码>@pictures
,根据您的代码,是@pictures=@micropost.pictures
。是的,我从未想过,我可以调用@pictures上的save,因为它包含@micropost变量。调用save时,这不会重复保存图片?Ie在(micropost_params)
和@pictures块上?如果不检查图片是否存在,它将复制。我已经用“验证”更新了代码,验证了我目前在我的应用程序中使用的回形针。好的,我知道你在做什么,如果我决定这样做,我可能会实现这一点!嘿,我以为它没有返回@micropost对象,我想我应该试试看。我一定是弄错了返回对象意味着什么?虽然这会“起作用”,但如果在更新图片时出现错误,用户将不知道,并且会在后台无声地失败。