简体   繁体   中英

Michael Hartl's Rails Tutorial Chapter 10 destroy method

I have a conceptual rails question. I am currently working on Micahel Hartl famous's Rails tutorial ( http://ruby.railstutorial.org/ ) and I'm up to chapter 10 (woot!). As a bit of background, microposts are being created and a twitter like status feed is being implemented. When the a micropost is created, it appears in the home status feed and profile page. My question comes from there relationship between the microposts and the feed_item object. In the tutorial, a micropost can be deleted either through the user's profile or home page's feed. The thing is the microposts are deleted through different partials depending on whether its the user's profile or home page's feed. Here are the partials :

For the profile page:

  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: micropost.content %>

For the home page status feed:

<li id="<%= feed_item.id %>">

  <span class="user">
    <%= link_to feed_item.user.name, feed_item.user %>
  </span>
  <span class="content"><%= feed_item.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
  </span>
    <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: feed_item.content %>
  <% end %>
</li> 

Here are the controllers, views and models:

class StaticPagesController < ApplicationController
  def home
    if signed_in?
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

end

class User < ActiveRecord::Base
...
has_many :microposts,  dependent: :destroy
....
  def feed
    Micropost.where("user_id = ?", id)
  end
...
end

    class MicropostsController < ApplicationController
    before_action :signed_in_user, only: [:create, :destroy]
    before_action :correct_user,   only: :destroy
..

     def destroy
            @micropost.destroy
            redirect_to root_url
          end

...
    end

I assume that the micropost being deleted from the home page status feed is occurring through the Microposts Controller's destroy method, but I am unsure how. I see in the home page status feed that the link_to's delete button has the delete method, but it goes to the feed_item url. Where does this feed_item url come from? I assume that the link_to knows somehow to delete a micropost because the feed's original source was from an array of microposts in the User model, but how does it know to go to the Micropost's controller destroy method by hitting the feed_item url? Does the link_to "title: feed_item.content" have anything to do with the feed_item knowing to delete which micropost? If anyone could help me understand the relationship between the micropost, feed_item, and destroy method, I would be highly grateful. Thank you!

I can help you with DELETE method, it is pretty easy in fact: most browser don't support methods PATCH, PUT & DELETE so Rails cheat in a way by passing through the parameter "_method" and converting it internally. That is why you see it as a GET, which it is until it hits Rails internals.

You can read more here: http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM