assembler / attachinary

Attachments handler for Rails that uses Cloudinary for storage.
MIT License
294 stars 129 forks source link

Rails 5 create action requires save before adding attachments #130

Open jakobdamjensen opened 8 years ago

jakobdamjensen commented 8 years ago

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.