ruby-on-rails - 销毁操作中的重定向无法正常工作

标签 ruby-on-rails ruby redirect crud destroy

我正在使用 Ruby on Rails 构建一个简单的博客应用程序,允许用户登录/注销、注册并根据权限和限制对他们的文章和个人资料执行操作。 我遇到了销毁用户操作的问题。在用户/索引 View (其中列出了所有现有用户)中,由于 url 路径不包含 {:id},它不会导致错误,但 redirect_to root_path 不起作用。如果在用户/显示页面(包含一些信息和相关文章的个人资料页面)中执行相同的操作,由于 url 是 localhost/users/id,当用户被删除时,我得到“Couldn't find 'id'=33 的用户”错误如下所示。如果我手动转到根路由,则会显示成功删除帐户的消息,并且会正确执行操作。所以这不是 DESTROY 不起作用的问题,而是我相信的重定向问题。我已经尝试重定向到不同的路径,但它仍然不起作用。以下是相关文件:

routes.rb

Rails.application.routes.draw do
root "pages#home"
get "about", to: "pages#about"

resources :articles

get "signup", to: "users#new"
resources :users, except: [:new] 

get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
get 'logout' => :destroy, to: 'sessions#destroy'
end

pages_controller

class PagesController < ApplicationController
def home
    redirect_to articles_path if logged_in?
end

def about
end         
end

users_controller

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :require_user, only: [:edit, :update]
before_action :require_same_user, only: [:edit, :update, :destroy]

def index
    @users = User.all
end

def show
    @articles = @user.articles
end

def new
    @user = User.new
end

def edit
end

def create
    @user = User.new(user_params)
    if(@user.save)
        session[:user_id] = @user.id  #logs user in automatically once they are signed up
        flash[:notice] = "Welcome to AlphaBlog, #{@user.username}!"
        redirect_to articles_path
    else
        render 'new'
    end
end

def update
    if @user.update(user_params)
        flash[:notice] = "Account updated!"
        redirect_to @user
    else
        render 'edit'
    end
end

def destroy
    @user.destroy
    session[:user_id] = nil
    flash[:notice] = "Account and all associated articles deleted!"
    redirect_to root_path
end

private
def user_params
    params.require(:user).permit(:username, :email, :password)
end

def set_user
    @user = User.find(params[:id])
end

def require_same_user
    if current_user != @user
        flash[:alert] = "You can only edit your own profile!"
        redirect_to current_user
    end
end

end

sessions_controller

class SessionsController < ApplicationController

def new
end

def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
        session[:user_id] = user.id
        flash[:notice] = "Logged in successfully!"
        redirect_to user
    else
        flash.now[:alert] = "There was something wrong with your login details!"
        render 'new'
    end

end

def destroy
    session[:user_id] = nil
    flash[:notice] = "Logged out."
    redirect_to root_path
end

end

users/index.html.erb

<div class = "header">
<h1>
    AlphaBlog
    <% if logged_in? %>
        <%= link_to 'Articles', articles_path, method: :get, class: "index-button-to" %>
    <% else %>
        <%= link_to 'Home', root_path(), method: :get, class: "index-button-to" %>
        <%= link_to 'Articles', articles_path, method: :get, class: "index-button-to" %>
    <% end %>
    <%= render 'layouts/log_in_out_navigation'%>
</h1>
</div>

<h2>Alpha Bloggers</h2>

<div class="index-container">
<%# cycle through all articles and show them all in a table %>
<% @users.each do |user| %>

    <div class = "index-article-container">

        <div class="index-article-user" style = "color:rgb(16, 136, 255);">
            <%= user.username %>
        </div>

        <div class="white">

            <div class="index-article-title">
                <%= gravatar_for(user, size: 150) %>
            </div>

            <div class="index-article-description">
                <%# gives the plural word for multiple articles %>
                <%= pluralize(user.articles.count, "article") %>
            </div>

            <div class="index-article-actions">
                <%# shows selected article page %>
                <%= link_to 'View Profile', user, class: "index-link-to show" %>
                <% if logged_in? && current_user.username == user.username %>
                    <%# shows selected article EDIT page. edit_article_path because in routes, 
the prefix for edit is edit_article && (article) because we need the id for the path as well%>
                    <%= link_to 'Edit Profile', edit_user_path(user), data: { turbo_method: 
:get}, class: "index-link-to edit" %>
                    <%= link_to 'Delete Profile', user_path(current_user), data: { 
turbo_method: :delete, turbo_confirm: "Are you sure? (This will also delete all of your 
articles)" }, class: "index-link-to delete" %>
                <% end %>
            </div>
                    
        </div>
                    

        <div class="index-created-updated">
            Joined <%= time_ago_in_words(user.created_at) %> ago.
        </div>

    </div>
    
<% end %>

users/show.html.erb

<div class = "header">
<h1>
    AlphaBlog
    <% if logged_in? %>
        <%= link_to 'Articles', articles_path, method: :get, class: "index-button-to" %>
        <%= link_to 'Bloggers', users_path, method: :get, class: "index-button-to" %>
    <% else %>
        <%= link_to 'Home', root_path(), method: :get, class: "index-button-to" %>
        <%= link_to 'Articles', articles_path, method: :get, class: "index-button-to" %>
        <%= link_to 'Bloggers', users_path, method: :get, class: "index-button-to" %>
    <% end %>
    <%= render 'layouts/log_in_out_navigation'%>
</h1>
</div>

<h2> <%= @user.username %>'s profile </h2>

<div class="show-users-image">
<%# gravatar_for method created in helpers/application_helper %>
<%= gravatar_for @user, size: 200 %>
<% if logged_in? && current_user.username == @user.username %>
    <div class="index-profile-actions">
        <%= link_to "Edit Profile", edit_user_path(@user), class: "index-link-to edit" %>
        <%= link_to 'Delete Profile', user_path(current_user), data: { turbo_method: :delete, 
turbo_confirm: "Are you sure? (This will also delete all of your articles)" }, class: "index- 
link- 
to delete", style: "margin-top:0.3vh" %>
    </div>
<% end %>
</div>

<h3 style = "text-align:center">Articles</h3>
<%= render 'articles/article' %>

错误页面 enter image description here

最佳答案

在 Rails 7 中执行此操作的方法是通过在重定向后添加 status::see_other 来更新 UsersController 中的 destroy 操作,如下:

def destroy
    @user.destroy
    session[:user_id] = nil
    flash[:notice] = "Account and all associated articles deleted!"
    redirect_to root_path, status: :see_other
end

关于ruby-on-rails - 销毁操作中的重定向无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71882837/

相关文章:

ruby-on-rails - 如何使用 'memcached' 在面向服务的体系结构中处理用户身份验证?

ruby - 创建 Ruby gem 的规范机制是什么?

ruby-on-rails - 查找所有返回相同数量的大于和小于 (> <) 的旧记录

http - 如何重写 Squid-Cache 内部 URL?

asp.net-mvc - 如何避免开放重定向漏洞并在成功登录时安全重定向(提示 : ASP. NET MVC 2 默认代码存在漏洞)

ruby-on-rails - 按哈希表中的值排序 - Ruby

html - CSS 悬停在图像上,即使它不包含在内

ruby-on-rails - Rails 中有哪些可用的 Flash 消息类型?

ruby - 获取哈希中重复键的计数

javascript - 重定向到 URL,然后在 "DOM Ready"时在新加载页面的上下文中运行一个函数