modernistik / parse-stack

Parse Server Ruby Client SDK
https://www.modernistik.com/gems/parse-stack/
MIT License
61 stars 20 forks source link

Edit method is creating new records, instead of just updating existing ones #34

Closed edudouglas closed 7 years ago

edudouglas commented 7 years ago

Thanks for the last answer, I found a problem when I was trying to validate the data before saving the edition, in my model, I defined something as: property: name, required: true when opening a record to edit and leave the empty "name" field goes to redirected to the "update" method, if I send the form again goes to the "create" method, the form path becomes "new_film" instead of "edit_film".

def update
    @film = Film.find params[:id]
    @film.attributes = params.require(:film).permit(:name)
    if @film.valid? 
      @film.save
      redirect_to @film
    else
      render 'edit'
    end
end
apersaud commented 7 years ago

This seems to be more of a rails-logic question instead of parse-stack specifically. From the sample above, you will most likely always go to redicrect_to @film since as long as valid? is true it will save the @film (regardless of success or failure` and go to the proper view. I would recommend using a more direct approach of (even testing on rails console or byebug).

attrs = # get attrs from params for :film
@film.name = attr[:name] # and inspect what you are getting
 if @film.valid? && @flim.save
     redirect_to @film
 else
    render 'edit'
 end

If you can provide a more detail exampled of an issue you are having with latest version of parse-stack, that would be best.

edudouglas commented 7 years ago

The problem is that after the model validates the data and verify that it has an error it goes to the "edit" method, however, this "edit" goes with the path as if it were to create a new record and consequently ends up going to the "create".

attrs = #get attrs from params for: film
@film.name = attr[:name] # and inspect what you are getting
  if @film.valid? && @flim.save
      redirect_to @film
  else
     # should go to /film/objectId/edit
     # and display errors on the screen with @film.errors
     # but it is going to /film/objectId
     # and submitting again goes to "create" instead of "update"
     render 'edit'
  end

It is not a problem of routes because I created a scaffold for testing with an sqlite database and everything works normally. Sorry for my english.