我正在研究这个在模型上使用继承的应用程序,但是它得到了这个错误消息关于视图:
超类BankTransfer不匹配
我已经创建了一个MVC名为"payment_methods"使用脚手架命令:
rails g scaffold payment_methods type:string is_active:boolean
在模型文件中,payment_method.rb,我包含了以下类:
class PaymentMethod < ApplicationRecord
end
class BankTransfer < PaymentMethod
end
class PayPal < PaymentMethod
end
使用rails控制台,使用命令:BankTransfer.create
和Paypal.create
,可以在数据库上创建对象。
所以运行在rails控制台:PaymentMethod.all
我得到返回(这表明我,它是正确保存在数据库上):
PaymentMethod.all
PaymentMethod Load (0.2ms) SELECT "payment_methods".* FROM "payment_methods"
=>
[#<Boleto:0x00007f995d403000
id: 1,
type: "BankTransfer",
is_active: nil,
created_at: Fri, 15 Jul 2022 16:46:29.759160000 UTC +00:00,
updated_at: Fri, 15 Jul 2022 16:46:29.759160000 UTC +00:00>,
#<Stripe:0x00007f995d402da8
id: 2,
type: "PayPal",
is_active: nil,
created_at: Fri, 15 Jul 2022 16:46:56.087921000 UTC +00:00,
updated_at: Fri, 15 Jul 2022 16:46:56.087921000 UTC +00:00>]
自动创建视图和控制器。
index.html.erb
<div id="payment_methods">
<% @payment_methods.each do |payment_method| %>
<%= render payment_method %>
<p>
<%= link_to "Show this payment method", payment_method %>
</p>
<% end %>
</div>
payment_methods_controller.rb
class PaymentMethodsController < ApplicationController
before_action :set_payment_method, only: %i[ show edit update destroy ]
def index
@payment_methods = PaymentMethod.all
end
def show
end
def new
@payment_method = PaymentMethod.new
end
(...)
private
def set_payment_method
@payment_method = PaymentMethod.find(params[:id])
end
def payment_method_params
params.require(:payment_method).permit(:type, :is_active)
end
end
我应该修复什么,以便能够在前端看到付款方式的列表。而不是错误消息超类不匹配的类.
如果你要在payment_methods.type
列中有一个'Stripe'
值,那么你需要有一个Stripe
类,看起来像这样:
class Stripe < PaymentMethod
# ...
end
其他payment_methods.type
值也一样。
问题是stripe
gem定义了module Stripe
,这就是你的超类mismatch&;错误来自。
最简单的解决方案是使用不同的名称为您的条纹类,也许StripePaymentMethod
(然后PayPalPaymentMethod
,BankTransferPaymentMethod
,…一致性)。