Rails 第 9 章教程帮助
Rails Tutorial Help on Chapter 9
我在第 9.2.2 节中有 运行 4 个错误。
错误
ERROR["test_unsuccessful_edit", UsersEditTest, 2015-11-05 04:35:59 -0600]
test_unsuccessful_edit#UsersEditTest (1446719759.23s)
NoMethodError: NoMethodError: undefined method correct_user?' for #<UsersController:0x007fcdf48ad378>
app/controllers/users_controller.rb:58:in
correct_user'
test/integration/users_edit_test.rb:10:in block in <class:UsersEditTest>'
app/controllers/users_controller.rb:58:in
correct_user'
test/integration/users_edit_test.rb:10:in `block in '
ERROR["test_successful_edit", UsersEditTest, 2015-11-05 04:35:59 -0600]
test_successful_edit#UsersEditTest (1446719759.28s)
NoMethodError: NoMethodError: undefined method correct_user?' for #<UsersController:0x007fcdefcea198>
app/controllers/users_controller.rb:58:in
correct_user'
test/integration/users_edit_test.rb:21:in block in <class:UsersEditTest>'
app/controllers/users_controller.rb:58:in
correct_user'
test/integration/users_edit_test.rb:21:in `block in '
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2015-11-05 04:35:59 -0600]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1446719759.42s)
NoMethodError: NoMethodError: undefined method correct_user?' for #<UsersController:0x007fcdf529f778>
app/controllers/users_controller.rb:58:in
correct_user'
test/controllers/users_controller_test.rb:29:in block in <class:UsersControllerTest>'
app/controllers/users_controller.rb:58:in
correct_user'
test/controllers/users_controller_test.rb:29:in `block in '
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2015-11-05 04:35:59 -0600]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1446719759.44s)
NoMethodError: NoMethodError: undefined method correct_user?' for #<UsersController:0x007fcdf531e118>
app/controllers/users_controller.rb:58:in
correct_user'
test/controllers/users_controller_test.rb:36:in block in <class:UsersControllerTest>'
app/controllers/users_controller.rb:58:in
correct_user'
test/controllers/users_controller_test.rb:36:in `block in '
32/32:[======================] 100% 时间:00:00:01,时间:00:00:01
在 1.70355 秒内完成
32 次测试,64 次断言,0 次失败,4 次错误,0 次跳过
Users_controller.rb
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
User_controller_test
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
users.yml
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') %>
users_edit_test
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
我认为这就是需要帮助的全部内容。我已经挣扎了一段时间了。这是我的第一篇文章post,如有不便请见谅。
correct_user?未在任何地方定义。
redirect_to(root_url) unless correct_user?(@user)
Rails 的语法糖是要查找模型标志 correct_user 还是返回布尔值的函数 #correct_user? getter。我假设您正在尝试验证来自用户的更新请求是否确实属于正确的用户。为此,您必须将 current_user 存储在会话中,然后检查请求参数 [:id] == current_user.id
我在第 9.2.2 节中有 运行 4 个错误。
错误
ERROR["test_unsuccessful_edit", UsersEditTest, 2015-11-05 04:35:59 -0600] test_unsuccessful_edit#UsersEditTest (1446719759.23s) NoMethodError: NoMethodError: undefined method
correct_user?' for #<UsersController:0x007fcdf48ad378> app/controllers/users_controller.rb:58:in
correct_user' test/integration/users_edit_test.rb:10:inblock in <class:UsersEditTest>' app/controllers/users_controller.rb:58:in
correct_user' test/integration/users_edit_test.rb:10:in `block in 'ERROR["test_successful_edit", UsersEditTest, 2015-11-05 04:35:59 -0600] test_successful_edit#UsersEditTest (1446719759.28s) NoMethodError: NoMethodError: undefined method
correct_user?' for #<UsersController:0x007fcdefcea198> app/controllers/users_controller.rb:58:in
correct_user' test/integration/users_edit_test.rb:21:inblock in <class:UsersEditTest>' app/controllers/users_controller.rb:58:in
correct_user' test/integration/users_edit_test.rb:21:in `block in 'ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2015-11-05 04:35:59 -0600] test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1446719759.42s) NoMethodError: NoMethodError: undefined method
correct_user?' for #<UsersController:0x007fcdf529f778> app/controllers/users_controller.rb:58:in
correct_user' test/controllers/users_controller_test.rb:29:inblock in <class:UsersControllerTest>' app/controllers/users_controller.rb:58:in
correct_user' test/controllers/users_controller_test.rb:29:in `block in 'ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2015-11-05 04:35:59 -0600] test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1446719759.44s) NoMethodError: NoMethodError: undefined method
correct_user?' for #<UsersController:0x007fcdf531e118> app/controllers/users_controller.rb:58:in
correct_user' test/controllers/users_controller_test.rb:36:inblock in <class:UsersControllerTest>' app/controllers/users_controller.rb:58:in
correct_user' test/controllers/users_controller_test.rb:36:in `block in '
32/32:[======================] 100% 时间:00:00:01,时间:00:00:01
在 1.70355 秒内完成 32 次测试,64 次断言,0 次失败,4 次错误,0 次跳过
Users_controller.rb
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
User_controller_test
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
users.yml
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') %>
users_edit_test
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
我认为这就是需要帮助的全部内容。我已经挣扎了一段时间了。这是我的第一篇文章post,如有不便请见谅。
correct_user?未在任何地方定义。
redirect_to(root_url) unless correct_user?(@user)
Rails 的语法糖是要查找模型标志 correct_user 还是返回布尔值的函数 #correct_user? getter。我假设您正在尝试验证来自用户的更新请求是否确实属于正确的用户。为此,您必须将 current_user 存储在会话中,然后检查请求参数 [:id] == current_user.id