Save 36% for Black Friday! Learn more

All threads / Rails for Beginners Part 32: Edit and Destroy Tweets Discussion
Ask A Question

Notifications

You’re not receiving notifications from this thread.

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?

Reply

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.

Reply

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.

Reply

don't worry guys, later on, he fixed it changing the button_to for a be a link_to, and the problem solved.

Reply

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.

Reply

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" %>

Thank you for this! Definitely ran into it. Works well. Also allows me to fix another issue I was having on a previous video.

Reply

@Andrew Ah thanks! I had to create a button_to outside of the form as a work-around but this is much better.

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 67,819+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.