Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 5 create action requires save before adding attachments #130

Open
jakobdamjensen opened this issue Jun 15, 2016 · 0 comments · May be fixed by #134
Open

Rails 5 create action requires save before adding attachments #130

jakobdamjensen opened this issue Jun 15, 2016 · 0 comments · May be fixed by #134

Comments

@jakobdamjensen
Copy link

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.

@ddomingues ddomingues linked a pull request Aug 24, 2016 that will close this issue
fabn added a commit to fabn/attachinary that referenced this issue Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant