- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在按照 Agile Web Development with Rails 4 学习 Rails。我在第 9 章,刚刚完成单元测试。为了更好地理解我正在阅读的内容,我正在逐步构建我自己的应用程序,以及我必须按照本书构建的应用程序。这本书讲述了建立一个电子商店的过程,我的应用程序是一个病人记录保存应用程序。
我正在编写测试。我生成了两个脚手架 Doctors
和 Patients
。对于 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)
是我的常客。
最后 Doctor
的 controller
是:
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/
目录 count作用 测试 count(*) count(1) count(col) count(id):统计id count(inde
目录 1.初识COUNT 2.COUNT(字段)、COUNT(常量)和COUNT(*)之间的区别 3.COUNT(*)的优化 MyIS
以下 SQL Server 2008 语句之间有什么区别? SELECT COUNT(*) FROM dbo.Regular_Report SELECT COUNT(0) FROM dbo.Regul
如果字符串(短语)中只有元音,它(对我而言)说True;否则说 False。我不明白为什么它总是返回 False,因为 (x >= x) 总是返回 True。我感谢任何人检查此查询的解决方案。 (st
1.概述 在这个文章之前,我一直用count(1) 查询所有数据,以前我们都是说 count(*) 是最慢的。但是这个博客恰恰相反。 对于 count(主键 id) 来说,InnoDB 引擎会遍历整张
这个问题已经有答案了: Count(*) vs Count(1) - SQL Server (13 个回答) 已关闭 8 年前。 我经常发现这三种变体: SELECT COUNT(*) FROM Fo
为什么三个查询的成本相同?我想至少应该有一个更快。否则,只使用关键字 COUNT() 而不是 COUNT(parameter) 就可以了。 例如,以下是不依赖于参数的 COUNT() 示例实现: wh
我有一个“产品”表和一个“评论”表。 我想编写一个查询来返回每个产品的评论的 COUNT 和 AVG。 并且如果没有评论,我希望它为 COUNT 和 AVG 返回 0/null。 产品表 +-----
我会保持简短和亲切,因为我确信我缺少的是一些简单的东西。我正在尝试获取一个 NSMutableArray 的计数,它可以包含可变数量的对象(id 号)。数组是从 JSon 数据创建的,数组本身是完美创
我想知道查询计数的计数。 查询是 sourcetype="cargo_dc_shipping_log" OR sourcetype="cargo_dc_deliver_log" | stats cou
任何人都知道我如何在 SQL 炼金术中进行计数 COUN(IF(table_row = 1 AND table_row2 =2),1,0) 我做了这样的东西, func.COUNT(func.IF((
我有一个有四列的表(销售); id, user_id, product_id, and date_added. 我需要统计某个用户已售出的具有特定 id 的产品数量,并获取该用户当月售出的产品总数。
我是来问这个问题的实现的 MYSQL count of count? 我的问题是将我从一个表中提取结果的结果联系起来,使用它们来查询同一数据库的另一个表 (抱歉,我不是强大的 xySQL)。 我有一个
这是我的查询 SELECT COUNT(*) as total, toys, date FROM T1 WHERE (date >= '2012-06-26'AND date '0') UNION
我有 2 个表:成员,订单。 Members: MemberID, DateCreated Orders: OrderID, DateCreated, MemberID 我想找出给定月份中新成员的数
我最近在一次采访中被问到这个问题。我在 mySQL 中尝试了这个,并得到了相同的结果(最终结果)。All 给出了该特定表中的行数。谁能解释它们之间的主要区别。 最佳答案 没什么,除非您在表格中指定字段
我有一个包含 2157 条记录的表,假设有 3 列(A、B、C),我知道在 A 列中有 2154 个不同的值。 使用连接到 BigQuery 的 Tableau Desktop(及其自身的功能),我得
我试图查看当天的车辆销量,并创建另外两个列来告诉我过去 10 天的销量和过去 20 天的销量。同一天和同一辆车可能有多个销售。我的目标是获取不同的车辆和日期并查看他们的销售数量。 N 天计数应与该行中
我有一个非常简单的问题。我想知道某个数据库行是否存在。 我通常使用: SELECT 1 FROM `my_table` WHERE `field_x` = 'something' 然后我获取结果: $
我想要的输出的描述:我想要两个线程 Gaurav 和 john 完成一个 while 循环(从 1 到 8),这样无论哪个线程启动 ist,都会运行 5 次迭代(即直到 count=5 ) ,然后进入
我是一名优秀的程序员,十分优秀!