ruby-on-rails - 模型订单的未定义方法“aasm_state”您是说什么?aasm_state=
问题描述
添加Aasm gem后,Rspec-rails 测试开始失败。所有基本设置都已完成。通过添加 aasm_state 列进行迁移。将状态添加到 Order 模型。
测试日志:
Searching book Visitor searches books by title
Failure/Error: order = Order.create
ActionView::Template::Error:
undefined method `aasm_state' for #<Order:0x0000000008e48740>
Did you mean? aasm_state=
但是测试中没有订单:
require 'spec_helper'
require 'rails_helper'
feature 'Searching book' do
before do
book = Book.create title: "Geralt"
author = Author.create full_name: "Swiss"
category = Category.create title: "Programming"
category.books << book
book.authors << author
author.books << book
visit books_path
end
scenario 'Visitor searches books by title' do
fill_in 'search', with: "Geralt"
click_button('Search')
expect(page).to have_content 'Geralt'
end
end
也许问题出在应用程序控制器辅助方法中:
class ApplicationController < ActionController::Base
helper_method :current_order
protect_from_forgery with: :exception
def current_order
if session[:order_id].nil?
order = Order.create
session[:order_id] = order.id
order
else
Order.find(session[:order_id])
end
end
end
带有 aasm 的 Order.rb,部分:
include AASM
aasm do
state :in_progress, initial: true
state :processing
state :in_delivery
state :delivered
state :canceled
end
模板:
= form_tag books_path, method: :get do
.form-group.col-sm-2
= text_field_tag :search, params[:search], class: "form-control", id: "search",placeholder: "Search book"
= submit_tag "Search", class: "btn btn-primary"
= render @books
= "Items in cart #{current_order.order_items.count}"
解决方案
推荐阅读
- java - 如何从 Firebase Realtime DB 的键中获取所有值?
- flutter - Flutter 如何遍历映射和打印值
- c# - Mysql 和 Visual Studio 2019 for macOS 之间的连接不起作用
- tomcat - 在axis2.xml中检索当前工作目录
- pact-jvm - 无法发布到 Pack-Broker:重复键值违规
- php - laravel 5.8:在个人资料页面中向新用户显示空字段
- vb.net - VB.NET 静态变量存储在哪里?
- dns - 在 Windows Server 2019 AD 中创建子 DNS 时,父 dns 是服务器 2008 先决条件检查失败
- php - 显示数据的问题,来自 laravel 视图中的 sqlite?
- c# - 如何在 C# 中获取拉普拉斯算子的方差