Home>

I want to delete user
destroy action doesn't work

No error message is displayed

Corresponding source code users_controller
class UsersController</pre>
<strong>drinks/index.html.erb</strong>
<pre><code data-language = "erb"><% = link_to "Unsubscribe", user_path (current_user), method:: delete%>

current_user is a self-defined helper method

sessions_helper.rb
# Get information about the currently logged in user
  def current_user
    #I want to keep the number of DB inquiries as small as possible
    It is also used in the # logged_in? method ...
    if user_id = session [: user_id]
      #If i have a session
      # That is, only when i am logged in
      The result of accessing #session is used as a variable
      # It is better to put it in and reuse it later
      # Get faster
      @current_user || = User.find_by (id: user_id)
      Query the database with # find_by
      #User definition based on user_id in browser session
      Save the execution result of # find_by in an instance variable
      # By doing so, to the database in one request
      #Inquiry is only the first time,
      # Subsequent calls will take the result of the instance variable
      # Reuse
      # What if @current_user already exists?
      # Once you run current_user,
      There is # @current_user so please use it
      The fact that there is a user_id for # session
      # You are already logged in and there should be user information in the DB.
      # So just set the user_id of session to find_by in DB
    elsif (user_id = cookies.signed [: user_id])
      If # session is not posted, it may be in cookies
      user = User.find_by (id: user_id)
      if user&&user.authenticated? (cookies [: remember_token])
        # nil guard
        # Cookie user_id and remember_token match
        log_in user
        @current_user = user
      end
    end
  end
user.rb
class User</pre>
<strong>Drink.rb</strong>
<pre><code data-language = "Ruby">class Drink</pre>
<strong>: 20201116073717_create_users.rb</strong>
<pre><code>class CreateUsers</pre>
<strong>/ 20201118162753_create_drinks.rb</strong>
<pre><code data-language = "Ruby">class CreateDrinks</pre>
<strong>routes.rb</strong>
<pre><code data-language = "Ruby">Rails.application.routes.draw do
  root to:'drinks # index'
  get'/ login', to:'sessions # new'
  post'/ login', to:'sessions # create'
  delete'/ logout', to:'sessions # destroy'
  resources: users
  resources: drinks, only: [: index,: new ,: show ,: create,: destroy]
end

Moved unsubscribe link to users/show

No error has occurred, @user is well defined, and @ user.name can be displayed properly, so I think that the link is specified.

Looking at this log it looks like I'm back to root after following the link

Supplementary information (FW/tool version, etc.)

ruby 2.6.5
rails 6.0.3

  • Answer # 1

    before_action: logged_in_user, only: [: index ,: edit ,: update ,: destroy]
      before_action: correct_user, only: [: edit,: update ,: destroy]
      def correct_user
          redirect_to (root_url) unless current_user? (@user)
        end
    def logged_in_user
        unless logged_in?
          store_location
          #Remember the page the user wanted to go to
          flash [: danger] = "Please log in"
          redirect_to login_url
        end
      end

    Cause set destroy before_action
    If you remove: destroy, it's ok