Rails for Beginners Part 32: Edit and Destroy Tweets Discussion
Oddity I'm running in to, but can't see why. Once the delete button is added via .persist?, the schedule button ALSO seems to delete the tweet as well (without any warning). If the persisted? is removed the update works as it should, but the persist block readded deletes the tweet instead of updating it?
I think it is because tweet_params in the video Cris explain that when we click edit tweet it is only in memory maybe is that.
I do not know if it is the right way to do it . I comment the if form.object.persisted? and add this in the edit
<%= form_with model: @tweet do |form| %>
<% if form.object.persisted? %>
<%= button_to "Delete", form.object, method: :delete, data: {confirm: "Are you sure?"}, class: "btn btn-outline-danger" %>
<%end%>
<%end%>
But now my button is not side by side.
don't worry guys, later on, he fixed it changing the button_to for a be a link_to, and the problem solved.
Is it an HTML form rule that there can only be one button per form? You can make the links look like buttons with CSS, but I think an HTML form can only have one button.
I think for sending information it is true, maybe we need to put this button to do some action.
This works, a link styled as a button:
<% if form.object.persisted? %>
<%= link_to "Delete", tweet_path(tweet), method: :delete, class: "btn btn-outline-danger", data: {confirm: 'Confirm deletion?'} %>
<% end %>
If anyone notices that the tweets get deleted also when they are updated, this code fixes it also for Rails 7
// tweets_controller.rb
def destroy
@tweet.destroy
redirect_to tweets_path, notice: 'Tweet was unscheduled', status: :see_other. <--note this is for rails 7 for it will route to the show page by default
end
// _form.html.erb
<%= link_to "Delete", tweet_path(@tweet), data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'}, class: "btn btn-outline-danger" %>