简体   繁体   English

在Rails 4中使用gmail gem

[英]Using gmail gem with Rails 4

I'm trying to use the nu7hatch/gmail gem with rails and I've run into some problems. 我正在尝试将nu7hatch / gmail gem与rails一起使用,但遇到了一些问题。 To rule out a conflict with the rest of my app I started a new c9.io instance with an absolute minimum of code and still got the same problem. 为了排除与应用程序其余部分的冲突,我使用绝对最少的代码启动了一个新的c9.io实例,但仍然遇到相同的问题。 I changed the basic Rails scaffold as follows: 我将基本的Rails脚手架更改如下:

Gemfile (added): Gemfile(添加):

gem 'gmail'

routes.rb (added): route.rb(已添加):

root 'welcome#index'

welcome_controller.rb: welcome_controller.rb:

class WelcomeController < ApplicationController
  def index
    Gmail.connect("username", "password") do |gmail|
      @emails=gmail.inbox.find(:unread)
    end
  end
end

index.html.erb (view): index.html.erb(视图):

<%= @emails.each do |email| %>
<p><%= email.subject %></p>
<% end %>

When I visit the root of my app, I get the following error: cannot load such file -- mime/message 当我访问应用程序的根目录时,出现以下错误:无法加载此类文件-mime / message

Rails tell me that this error refers to the line: @emails=gmail.inbox.find(:unread) Rails告诉我,此错误涉及以下行:@ emails = gmail.inbox.find(:unread)

Does anyone have any ideas how I can make this work please? 有谁知道我如何进行这项工作?

I'm not sure if you've answered this by now or not, but I thought I would add what I found. 我不确定您是否已经回答了这个问题,但我想我会补充发现的内容。 I think the correct way to do it would be to set up something in config/initializer like what omniauth does. 我认为正确的方法是像omniauth一样在config / initializer中进行设置。 But I'm just using the gem for a small personal project so, I decided to build a gmail_client model, and tell the controller to use that. 但是我只是将gem用于一个小型个人项目,因此,我决定构建gmail_client模型,并告诉控制器使用该模型。

//gmail_client.rb

require 'active_model'

class GmailClient
  include ActiveModel::Model

  def build
    Gmail.connect!('some_email', 'some_password')
  end

end

I decided to go with ActiveModel because I didn't want this sort of thing stored in a DB. 我决定使用ActiveModel,因为我不想将这种东西存储在数据库中。

And then in the controller do something like: 然后在控制器中执行以下操作:

//some_controller
...

def index
  gmail = GmailClient.new.build
  @emails = gmail.inbox.find(:unread)
end

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM