在Rails中使用Google云端硬盘电子表格时“表格不存在”错误

时间:2013-11-16 04:02:32

标签: ruby-on-rails ruby google-sheets

我遵循Daneil Kehoe的指南,使用Google Drive电子表格为Rails应用程序创建在线数据库,但我遇到了麻烦。我在尝试为'Notes'控制器的'index'视图编写迭代器时遇到了这个问题。我收到了这个错误:

undefined method `each' for Note(Table doesn't exist):Class

使用此代码:

<tbody>
  <% Note.each do |note|%>
    <tr>
      <th><%= note.title %></th>
      <th><%= note.contents %></th> 
    </tr>
  <% end %>
</tbody>

这是我的'note.rb'文件,其中电子表格配置信息是

class Note < ActiveRecord::Base
    has_no_table
    column :title, :string
    column :contents, :string
    def update_spreadsheet
        connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
        ss = connection.spreadsheet_by_title('central-app-notes')
        if ss.nil?
        ss = connection.create_spreadsheet('central-app-notes')
        end
        ws = ss.worksheets[0]
    last_row = 1 + ws.num_rows
    ws[last_row, 1] = Time.new
    ws[last_row, 2] = self.title
    ws[last_row, 3] = self.content
    ws.save
    end
end

我正在使用存储在application.yml文件中的Figaro gem的模糊登录详细信息。我很确定google驱动器电子表格配置无法正常工作,因为我在Google云端硬盘中找不到一个电子表格(是的,我已登录到正确的帐户)。

这是我在NotesController中的'index'动作:

def index
        @note = Note.all

如果你想在项目周围逛一下,你可以去这里: https://github.com/MaxPleaner/someapp 要查看相关错误,请单击根页面上的第一个“Notes”链接。

2 个答案:

答案 0 :(得分:1)

好的,链接到教程有帮助。因此,使用此gem,您不再通过ActiveRecord方法访问数据。

您需要手动将其从电子表格中拉出来,而不是Note.each来访问您的笔记。因此,在您的控制器中,执行以下操作:

def index
  connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
  spreadsheet = connection.spreadsheet_by_title('central-app-notes')
  @ws = spreadsheet.worksheets[0]
end

在您看来,通过@ws变量访问您的数据。我对此谷歌电子表格宝石并不熟悉,但看起来您可以使用@ws.cells访问您的数据,或者您可以使用@ws.spreadsheet获取Google电子表格对象

您必须使用@ws对象并查看哪些数据对您最有用(通过@ws.methods探索可用的方法)。这应该会让你超越当前的驼峰

答案 1 :(得分:0)

在您的应用中查看,您需要在config / application.yml中为google gem设置google_drive gem的配置

GMAIL_USERNAME: Your_Username
GMAIL_PASSWORD: Your_Password

我在你的问题中知道你说你做了,但查看你的应用https://github.com/MaxPleaner/someapp并且配置文件夹中没有application.yml。

另外,在notes / index.html.erb视图中,我会使用@notes而不是Note,因为你从控制器传入了该实例变量。

<tbody>
  <% @notes.each do |note|%>
    <tr>
      <th><%= note.title %></th>
      <th><%= note.contents %></th> 
    </tr>
  <% end %>
</tbody>

希望这有帮助