Adding a delete link for nested attributes

后端 未结 3 745
情话喂你
情话喂你 2021-01-18 04:46

I have nested attributes in my show.erb and I create a blank nested attribute and show a grid of items with the blank at the bottom like so.

<%= form_for          


        
3条回答
  •  囚心锁ツ
    2021-01-18 05:28

    For those finding this from Google, while technically a delete checkbox might be the correct way, I think it is confusing - the only benefit is that it requires two clicks to delete (select box and hit update). It might be better to just ensure the delete is clear what is happening, and possibly add an easy way to get it back.

    The following is to send the update to the server as ajax, to handle updating the page in a callback or js view file. Remove remote: true and it will work as a regular link.

    # For my form I build a new object if it's missing,
    # so I need to check that this is not a new nested attribute.
    - unless question.answers.new_record?
      # Basically, I am sending over the fields that would be sent
      # by the _delete check box form being updated.
      = link_to "Delete", question_path(question.id, question: { answers_attributes: { id: question.answers.id, "_destroy" => true }}), remote: true, confirm: "Really delete?", method: :put
    

提交回复
热议问题