我在第9.2.2节遇到了4个错误。
错误[" test_unsuccessful_edit",UsersEditTest,2015-11-05 04:35:59 -0600] test_unsuccessful_edit #UsersEditTest(1446719759.23s) NoMethodError:NoMethodError:未定义的方法
correct_user?' for #<UsersController:0x007fcdf48ad378> app/controllers/users_controller.rb:58:in
correct_user&#39; test / integration / users_edit_test.rb:10:inblock in <class:UsersEditTest>' app/controllers/users_controller.rb:58:in
correct_user&#39; test / integration / users_edit_test.rb:10:在`block in&#39;错误[&#34; test_successful_edit&#34;,UsersEditTest,2015-11-05 04:35:59 -0600] test_successful_edit #UsersEditTest(1446719759.28s) NoMethodError:NoMethodError:未定义的方法
correct_user?' for #<UsersController:0x007fcdefcea198> app/controllers/users_controller.rb:58:in
correct_user&#39; test / integration / users_edit_test.rb:21:inblock in <class:UsersEditTest>' app/controllers/users_controller.rb:58:in
correct_user&#39; test / integration / users_edit_test.rb:21:在`block in&#39;ERROR [&#34; test_should_redirect_edit_when_logged_in_as_wrong_user&#34;,UsersControllerTest,2015-11-05 04:35:59 -0600] test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest(1446719759.42s) NoMethodError:NoMethodError:未定义的方法
correct_user?' for #<UsersController:0x007fcdf529f778> app/controllers/users_controller.rb:58:in
correct_user&#39; test / controllers / users_controller_test.rb:29:inblock in <class:UsersControllerTest>' app/controllers/users_controller.rb:58:in
correct_user&#39; test / controllers / users_controller_test.rb:29:在`block in&#39;ERROR [&#34; test_should_redirect_update_when_logged_in_as_wrong_user&#34;,UsersControllerTest,2015-11-05 04:35:59 -0600] test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest(1446719759.44s) NoMethodError:NoMethodError:未定义的方法
correct_user?' for #<UsersController:0x007fcdf531e118> app/controllers/users_controller.rb:58:in
correct_user&#39; test / controllers / users_controller_test.rb:36:inblock in <class:UsersControllerTest>' app/controllers/users_controller.rb:58:in
correct_user&#39; test / controllers / users_controller_test.rb:36:在`block in&#39;
32/32:[======================] 100%时间:00:00:01,时间:00:00:01
在1.70355s完成 32个测试,64个断言,0个失败,4个错误,0个跳过
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before Filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless correct_user?(@user)
end
end
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get :new
assert_response :success
end
test "should redirect edit when logged in" do
get :edit, id: @user
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(@other_user)
get :edit, id: @user
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert flash.empty?
assert_redirected_to root_url
end
end
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
require 'test_helper'
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsuccessful edit" do
log_in_as(@user)
get edit_user_path(@user)
assert_template 'users/edit'
patch user_path(@user), user: { name: "",
email: "foo@invalid",
password: "foo",
password_confirmation: "bar" }
assert_template 'users/edit'
end
test "successful edit" do
log_in_as(@user)
get edit_user_path(@user)
assert_template 'users/edit'
name = "Foo Bar"
email = "foo@bar.com"
patch user_path(@user), user: { name: name,
email: email,
password: "",
password_confirmation: "" }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal name, @user.name
assert_equal email, @user.email
end
end
我认为只需要帮助就可以了。我一直在苦苦挣扎一段时间。这是我的第一篇文章,对不起,如果它不容易阅读。
答案 0 :(得分:1)
redirect_to(root_url) unless correct_user?(@user)
Rails的语法糖将寻找模型标志correct_user或布尔返回函数#correct_user?吸气。我假设您正在尝试验证来自用户的更新请求实际上属于正确的用户。为此,您必须将current_user存储在会话中,然后检查请求参数[:id] == current_user.id