首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails中的冲突类名

Rails中的冲突类名
EN

Stack Overflow用户
提问于 2018-05-29 17:29:26
回答 1查看 156关注 0票数 1

我正在尝试使用sendgrid-ruby API发送电子邮件。我有一个Attachment模型,用于跟踪AWS桶中上传的文件。

我只是尝试在设置后运行bundle安装

代码语言:javascript
复制
gem 'sendgrid-ruby' 

在我的Gemfile中,但是在安装gem之后加载应用程序时,我会得到以下错误:

代码语言:javascript
复制
TypeError (superclass mismatch for class Attachment):

app/models/attachment.rb:1:in `<top (required)>'
app/controllers/jobs_controller.rb:182:in `edit'

不管怎么说,在不更改我的模型名称的情况下,是否可以修复这个问题?

更新:

我的SendGridEmail课程:

代码语言:javascript
复制
require 'sendgrid-ruby'
include SendGrid

class SendGridEmail

  def initialize 
  end

  def send_message
    mail = Mail.new
    mail.from = Email.new(email: 'test@example.com')
    mail.subject = 'I\'m replacing the subject tag'
    personalization = Personalization.new
    personalization.add_to(Email.new(email: 'cannon.moyer@treadmilldoctor.com'))
    personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User'))
    personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver'))
    mail.add_personalization(personalization)
    mail.add_content(Content.new(type: 'text/html', value: 'I\'m replacing the <strong>body tag</strong>'))
    mail.template_id = '7fd8c267-9a3c-4093-8989-3df163e87c47'

    sg = SendGrid::API.new(api_key: "xxxxxxxxxxxxxxxxxxxxxx")
    begin
        response = sg.client.mail._("send").post(request_body: mail.to_json)
    rescue Exception => e
        puts e.message
    end
    puts response.status_code
    puts response.body

    puts response.headers
  end

  def create_notification
    @customer.notifications.create(name: "Manually Text Job to Customer", to: "+1", notification_type: "Manual Job Notification")
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-29 18:19:51

使用include会扰乱名称空间,所以最好不要这样做。Ruby有一个独立的全局命名空间,与Python和Node不同,因此在导入时需要非常小心。

如果您的include SendGrid有一个SendGrid::Attachment类,那么SendGrid::Attachment将尝试与您的Attachment类进行协调,这将失败。

最好的计划是更新您的代码,以便在所有内容中都包含SendGrid::前缀。一个设计良好的库将被清晰的命名空间以避免这个问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50589869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档