gpt4 book ai didi

ruby-on-rails - Rails 4 'permit' 和 'count by one' 错误

转载 作者:行者123 更新时间:2023-11-28 20:55:42 25 4
gpt4 key购买 nike

我正在按照 Agile Web Development with Rails 4 学习 Rails。我在第 9 章,刚刚完成单元测试。为了更好地理解我正在阅读的内容,我正在逐步构建我自己的应用程序,以及我必须按照本书构建的应用程序。这本书讲述了建立一个电子商店的过程,我的应用程序是一个病人记录保存应用程序。

我正在编写测试。我生成了两个脚手架 DoctorsPatients。对于 Patients 来说,一切都很顺利,因为我实际上是在以与本书并行的方式进行所有操作。

不过,对于 Doctors,我遇到了一个问题,其中 app/controller/doctors_controller.rb 中的 [params] 传递了有效负载作为 String 而不是 Hash。更具体地说,我得到的错误是:

DoctorsControllerTest
ERROR (0:00:00.094) test_should_create_doctor
undefined method `permit' for "625417172":String
@ app/controllers/doctors_controller.rb:72:in `doctor_params'
app/controllers/doctors_controller.rb:27:in `create'
[...]

我仔细检查了代码,一切都与 Patients Controller 相似,但我没有遇到任何问题!在做了一些 online research 之后,我设法使用 attributes 绕过了这个错误:

[...]
test "should create doctor" do
assert_difference('Doctor.count') do
post :create, doctor: @doctor.attributes # instead of @doctor
end

assert_redirected_to doctor_path(assigns(:doctor))
end
[...]

但是当我运行 rake 测试 时出现以下错误:

DoctorsControllerTest
FAIL (0:00:00.182) test_should_create_doctor
"Doctor.count" didn't change by 1.
Expected: 3
Actual: 2

如果有人可以通过解决第一个报告的错误(恕我直言,这是我问题的真正根源)或至少是第二个错误来给我一些关于如何通过此测试的提示,我会很高兴继续我的项目。

编辑:共享请求的代码:

Doctor 的模型是:

class Doctor < ActiveRecord::Base

validates :name, presence: {message: "Συμπληρώστε το όνομα του ιατρού"}
validates :surname, presence: {message: "Συμπληρώστε το επώνυμο του ιατρού"}
validates :birthday, presence: {message: "Συμπληρώστε την ημερομηνία γεννήσεως ιατρού"}
validates :gender, presence: {message: "Συμπληρώστε το γένος του ιατρού"}
validates :identity_number, presence: {message: "Συμπληρώστε τον αριθρμό αστυνομικής ταυτότητας του ιατρού"}
validates :identity_number_pd, presence: {message: "Συμπληρώστε την πόλη του αστυνομικού τμήματος που εξέδωσε την ταυτότητα"}
validates :father_name, presence: {message: "Συμπληρώστε το πατρώνμου ιατρού"}
validates :doctor_specialty, presence: {message: "Συμπληρώστε την ειδικότητα του ιατρού"}
validates :doc_medical_association_city, presence: {message: "Συμπληρώστε την πόλη του ιατρικού συλλόγου που είναι εγγεγραμένος ο ιατρός"}
validates :home_phone, presence: {message: "Συμπληρώστε τον αριθμό τηλεφώνου οικίας του ιατρού"}
validates :mobile_phone, presence: {message: "Συμπληρώστε το κινητό τηλέφωνο του ιατρού"}
validates :city, presence: {message: "Συμπληρώστε την πόλη κατοικίας του ιατρού"}
validates :country, presence: {message: "Συμπληρώστε χώρα της κατοικίας του ιατρού"}
validates :postal_code, presence: {message: "Συμπληρώστε τον ταχυδρομικό κώδικα της οικίας του ιατρού"}
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i, message: "Η μορφή της φωτογραφίας πρέπει να είναι PNG, JPG ή GIF"
}
validates :email, :allow_blank => true,:uniqueness => { :case_sensitive => false }, :email_format => {:message => 'Η διεύθυνση email που έχετε εισαγάγει είναι λανθασμένη'}

# Εξωτερικές συναρτήσεις (methods) για validation
validate :birthday_is_date # γεννέθλια
validate :doc_medical_association_no_check

# Έλεγχος ορθότητας ημερομηνίας γεννήσεως. Στην βάση δεδομένων η ημερομηνίες θα σωθούν με το Αμερικάνικο σύστημα
# πιο συγκεκριμένα: Χρονιά/μήνα/ημέρα, π.χ. 1972/11/24
def birthday_is_date
errors.add(:birthday, "Λάθος στην ημερομηνία γεννήσεως!") unless Chronic.parse(birthday)
end

# Έλεγχος για μοναδικό άριθμο μητρώου ιατρού. Εδώ το validation μπορεί να δημιουργήσει πρόβλημα. Πρέπει να εισαχθεί και τρίτο 'condition' - 14/03/14
def doc_medical_association_no_check
if doc_medical_association_no
errors.add(:doc_medical_association_no, "Ο αριθμός μητρώου υπάρχει είδη στην βάση δεδομένων!") if Doctor.exists?(["doc_medical_association_no = ? and not surname = ?", self.doc_medical_association_no, self.surname])
end
end
end

Doctor 的测试 Controller 是:

require 'test_helper'

class DoctorsControllerTest < ActionController::TestCase
setup do
@doctor = doctors(:alex)

@update = {
name: "Απόστολος",
surname: "Παπαδόπουλος",
gender: "Άνδρας",
birthday: Date.parse('1981-08-01'),
identity_number: "ΑT12314",
identity_number_pd: "Ξάνθης",
image_url: "ap_pap.jpg",
father_name: "Στέλιος",
doctor_specialty: "Πνευμονολόγος",
doc_medical_association_city: "ΑΘήνα",
doc_medical_association_no: "12345",
home_phone: "+30 1234567",
mobile_phone: "+30 1234567",
home_address: "Τεστ 32",
city: "Αθήνα",
country: "Ελλάδα",
postal_code: "12345",
email: "papa@somemail.com"
}
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:doctors)
end

test "should get new" do
get :new
assert_response :success
end

test "should create doctor" do
assert_difference('Doctor.count') do
post :create, doctor: @doctor # or @doctor.attributes but with the 'count by 1' error
end

assert_redirected_to doctor_path(assigns(:doctor))
end

test "should show doctor" do
get :show, id: @doctor
assert_response :success
end

test "should get edit" do
get :edit, id: @doctor
assert_response :success
end

test "should update doctor" do
patch :update, id: @doctor, doctor: @update
assert_redirected_to doctor_path(assigns(:doctor))
end

test "should destroy doctor" do
assert_difference('Doctor.count', -1) do
delete :destroy, id: @doctor
end

assert_redirected_to doctors_path
end
end

当然 doctor(:alex) 是我的常客。

最后 Doctorcontroller 是:

class DoctorsController < ApplicationController
before_action :set_doctor, only: [:show, :edit, :update, :destroy]

# GET /doctors
# GET /doctors.json
def index
@doctors = Doctor.all
end

# GET /doctors/1
# GET /doctors/1.json
def show
end

# GET /doctors/new
def new
@doctor = Doctor.new
end

# GET /doctors/1/edit
def edit
end

# POST /doctors
# POST /doctors.json
def create
@doctor = Doctor.new(doctor_params)

respond_to do |format|
if @doctor.save
format.html { redirect_to @doctor, notice: 'Doctor was successfully created.' }
format.json { render action: 'show', status: :created, location: @doctor }
else
format.html { render action: 'new' }
format.json { render json: @doctor.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /doctors/1
# PATCH/PUT /doctors/1.json
def update
respond_to do |format|
if @doctor.update(doctor_params)
format.html { redirect_to @doctor, notice: 'Doctor was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @doctor.errors, status: :unprocessable_entity }
end
end
end

# DELETE /doctors/1
# DELETE /doctors/1.json
def destroy
@doctor.destroy
respond_to do |format|
format.html { redirect_to doctors_url }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_doctor
@doctor = Doctor.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def doctor_params
params.require(:doctor).permit(:name, :surname, :gender, :birthday, :identity_number, :identity_number_pd, :image_url, :father_name, :doctor_specialty, :doc_medical_association_city, :doc_medical_association_no, :home_phone, :mobile_phone, :home_address, :city, :country, :postal_code, :email, :notes)
end
end

最佳答案

count by 1 问题显然是由不健康的验证引起的。我正在使用导致问题的电子邮件验证器。因此,如果有人遇到同样的问题,请尝试将 app/models/your_model_validations.rb 一个一个地禁用,然后看看效果如何。

关于ruby-on-rails - Rails 4 'permit' 和 'count by one' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22417453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com