Ruby on rails &引用;无法重定向到零&引用;
我将跟随Michael Hartl的rails教程并进行一些小的调整。用户可以在其配置文件上进行MicroPost(状态更新),此时页面将显示为重新加载,并显示其新状态。我可以亲自确认,这完全符合预期。问题是,由于某种原因,它的测试没有通过,即使测试之外的最终结果看起来很完美 microposts_controller.rb、microposts_interface_test.rb(失败测试)、相关错误消息和用户_controller.rb的屏幕截图,顺序如下:Ruby on rails &引用;无法重定向到零&引用;,ruby-on-rails,Ruby On Rails,我将跟随Michael Hartl的rails教程并进行一些小的调整。用户可以在其配置文件上进行MicroPost(状态更新),此时页面将显示为重新加载,并显示其新状态。我可以亲自确认,这完全符合预期。问题是,由于某种原因,它的测试没有通过,即使测试之外的最终结果看起来很完美 microposts_controller.rb、microposts_interface_test.rb(失败测试)、相关错误消息和用户_controller.rb的屏幕截图,顺序如下: microsposts\u co
microsposts\u controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Status updated!"
redirect_to @user
else
@feed_items = []
flash[:warning] = "Status was blank!"
redirect_to @user
end
end
def destroy
@micropost.destroy
flash[:success] = "Status deleted."
redirect_to @user
end
users\u controller.rb
:
require 'test_helper'
class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:mrtestit)
end
test "micropost interface" do
log_in_as(@user)
assert is_logged_in?
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
# Valid submission
content = "This status really ties the room together"
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content }
end
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
end
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.where(activated: true).paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
if logged_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
@microposts = @user.microposts.paginate(page: params[:page])
redirect_to root_url and return unless @user.activated?
end
class UsersController
我理解,出于某种原因,这意味着@user
是nil
。然而,我已经多次确认,重定向到@user
在其他任何地方都有效
事实上,每隔一次测试通过就已经证明了这一点,但为了进一步说明,我已经确认,失败的
重定向到@user
代码行在其他文件中也有效,例如当用户在其设置页面下更新其配置文件信息,然后重定向到其配置文件页面时。我已经花了10多个小时来做这件事(大部分是昨天的事),我即将删除这个测试,继续我的生活。Stackoverflow是我最后的选择。我想你必须改变
将_重定向到@user
到
将\u重定向到当前\u用户
@user
是类实例变量,必须手动创建,例如:
@user=user.find\u by\u id post.owner\u id
而当前用户
是全局变量,可以在任何地方使用。
但别忘了检查它为零,因为用户不能登录
您可以通过手动检查它,除非每个方法中都有当前用户.nil?
,或者更好地添加助手方法并在控制器中使用它,本教程已经介绍了这种方法:
模块会话助手
def已签名用户
除非你签了名?
门店位置
重定向至登录url,注意:“请登录。”
结束
结束
# ...
类MicropostsController<应用程序控制器
在\u操作之前:已登录\u用户
# ...
更新:
Aleks的建议是:如果您想重定向到post的所有者,而该所有者可能不是当前用户,请使用
user=micropost.owner
将_重定向到用户
我认为你必须改变
将_重定向到@user
到
将\u重定向到当前\u用户
@user
是类实例变量,必须手动创建,例如:
@user=user.find\u by\u id post.owner\u id
而当前用户
是全局变量,可以在任何地方使用。
但别忘了检查它为零,因为用户不能登录
您可以通过手动检查它,除非每个方法中都有当前用户.nil?
,或者更好地添加助手方法并在控制器中使用它,本教程已经介绍了这种方法:
模块会话助手
def已签名用户
除非你签了名?
门店位置
重定向至登录url,注意:“请登录。”
结束
结束
# ...
类MicropostsController<应用程序控制器
在\u操作之前:已登录\u用户
# ...
更新:
Aleks的建议是:如果您想重定向到post的所有者,而该所有者可能不是当前用户,请使用
user=micropost.owner
将_重定向到用户
有两种可能的解决方案
第一个是:
更改此项:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Status updated!"
redirect_to @user
else
@feed_items = []
flash[:warning] = "Status was blank!"
redirect_to @user
end
end
为此:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Status updated!"
redirect_to current_user
else
@feed_items = []
flash[:warning] = "Status was blank!"
redirect_to current_user
end
end
或者(如果第一种方法不起作用)第二种方法:
更新此行:
before_action :correct_user, only: :destroy
致:
有两种可能的解决方案 第一个是: 更改此项:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Status updated!"
redirect_to @user
else
@feed_items = []
flash[:warning] = "Status was blank!"
redirect_to @user
end
end
为此:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Status updated!"
redirect_to current_user
else
@feed_items = []
flash[:warning] = "Status was blank!"
redirect_to current_user
end
end
或者(如果第一种方法不起作用)第二种方法:
更新此行:
before_action :correct_user, only: :destroy
致:
请提供有问题的代码,而不是屏幕快照的链接。我已经按照你的要求完成了。请提供有问题的代码,而不是屏幕快照的链接。我已经按照你的要求完成了。你是对的!令人痛心的是,我几个小时前就说到了这一点,但我不确定这是否正确,所以我删除了它,并继续尝试其他事情。我这样做是因为出现了一条新的错误消息,我现在又看到了这条消息:我不确定为什么测试的最后一部分也失败了。这可能与作者将该行写成“assert_select'a',text:text:'delete',count:0}”有关,但这行不通,所以我删除了第二行“text:”和右括号。执行此操作时,语法错误消息消失,但代码失败。看起来测试用户可以访问其他用户。他是管理员吗?尝试限制对非管理员用户的访问。视图看起来有点像
通过测试几个不同的用户组合,我意识到第30行是失败的,而不是第31行。测试不是访问不同的用户,而是让用户保持在自己的页面上,因此他们总是可以在microposts.yml下看到他们拥有的microposts的数量,并且会有匹配数量的删除链接,因为他们是创建这些microposts的用户。我尝试通过添加follow\u重定向来修复此问题!到我下面的那一行