attachinary
attachinary copied to clipboard
Rails 5 create action requires save before adding attachments
I'm experiencing a weird issue. I've got a form which is shared between my update and create actions in Rails 5. Pretty standard pattern.
In my update action I can update the model in one step. Here's a simple examlpe showing the problem I'm facing:
def update
@bottle = find_bottle
if @bottle.update(bottle_params)
redirect_to @bottle
else
render :edit
end
end
In my create action however I need to add an additional step for it to work:
def create
new_bottle_params = bottle_params
photos = new_bottle_params.delete :photos
@bottle = Bottle.new new_bottle_params
@bottle.save
@bottle.update(photos: photos)
if @bottle.persisted?
redirect_to @bottle
else
render :new
end
end
If I don't split it up in create I get an error when saving saying that the photos are invalid.