I have followed Daneil Kehoe's guide for using Google Drive spreadsheets to make an online database for a Rails app, but I'm having trouble. I encountered this problem when trying to write an iterator for the 'index' view of a 'Notes' controller. I got this error:
undefined method `each' for Note(Table doesn't exist):Class
with this code:
<tbody>
<% Note.each do |note|%>
<tr>
<th><%= note.title %></th>
<th><%= note.contents %></th>
</tr>
<% end %>
</tbody>
Here is my 'note.rb' file where the Spreadsheet configuration info is
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
I am using obscured login details with the Figaro gem that are stored in my application.yml file. I am pretty sure the google drive spreadsheet configuration isn't working because I can't find a single spreadsheet in my Google Drive (yes, I'm logged into the right account).
Here's my 'index' action in the NotesController:
def index
@note = Note.all
end
If you want to poke around the project you can go here: https://github.com/MaxPleaner/someapp To get to the error in question, click the first 'Notes' link on the root page.
Ok, linking to the tutorial helped. So with this gem, you no longer access your data through the ActiveRecord methods.
Instead of doing Note.each
to access your notes, you need to manually pull them from the spreadsheet. So in your controller, do something like:
def index
connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
spreadsheet = connection.spreadsheet_by_title('central-app-notes')
@ws = spreadsheet.worksheets[0]
end
And in your view, access your data via the @ws variable. I'm not familiar with this google spreadsheet gem, but it looks like you can access your data with @ws.cells
or you can get a google spreadsheet object with @ws.spreadsheet
You'll have to play around with the @ws object and see what data is most useful for you (explore the methods available via @ws.methods
). This should get you past your current hump though