[英]Rails How to create a Model, Controller and View using Inheritance?
I am working on this application that uses inheritance on models, but it is getting this error message on the view:我正在开发这个在模型上使用继承的应用程序,但它在视图上收到以下错误消息:
superclass mismatch for class BankTransfer BankTransfer 类的超类不匹配
I have created an MVC called “payment_methods” Using the scaffold command:我使用脚手架命令创建了一个名为“payment_methods”的 MVC:
rails g scaffold payment_methods type:string is_active:boolean
In the model file, payment_method.rb , I have included the classes:在模型文件payment_method.rb中,我包含了以下类:
class PaymentMethod < ApplicationRecord
end
class BankTransfer < PaymentMethod
end
class PayPal < PaymentMethod
end
Using rails console, with the commands: BankTransfer.create
and Paypal.create
, was possible to create on database the objects.使用 rails 控制台,使用命令: BankTransfer.create
和Paypal.create
,可以在数据库上创建对象。
So running on rails console: PaymentMethod.all
i get the return (that shows me that it was correctly save on the database):所以在 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>]
The views and the controller were created automatically.视图和控制器是自动创建的。
index.html.erb 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 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
What should I fix to be able to see a list of payment methods on the frontend.我应该解决什么问题才能在前端看到付款方式列表。 Instead of the error message superclass mismatch for class .而不是错误消息superclass mismatch for class 。
If you're going to have a 'Stripe'
value in your payment_methods.type
column then you'll need to have a Stripe
class that looks like this:如果您要在payment_methods.type
列中有一个'Stripe'
值,那么您需要一个如下所示的Stripe
类:
class Stripe < PaymentMethod
# ...
end
Similarly for other payment_methods.type
values.对于其他payment_methods.type
值也是如此。
The problem is that the stripe
gem defines module Stripe
, that's where your "superclass mismatch" error comes from.问题是stripe
gem 定义了module Stripe
,这就是您的“超类不匹配”错误的来源。
The easiest solution would be to use a different name for your Stripe class, maybe StripePaymentMethod
(and then PayPalPaymentMethod
, BankTransferPaymentMethod
, ... for consistency).最简单的解决方案是为您的 Stripe 类使用不同的名称,可能StripePaymentMethod
(然后是PayPalPaymentMethod
、 BankTransferPaymentMethod
,......以保持一致性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.