Andre
Andre

Reputation: 366

Ruby on rails Class being mistaken as a Constant

First of all, I'm experimenting with Ruby on Rails for the first time. I'm doing a simple exercise of designing a form to receive events information and save those events in Google Calendar. To save the events on Google Calendar I'm using the following gem: http://googlecalendar.rubyforge.org/

I have the following code for the view.

<h1>Welcome to Ruby On Rails Calendar</h1>

<h3>Add Event</h3>
<form action="/google_calendar/create" method="post">
  <table>
    <tr>
      <td>Title</td><td><input type="text" /></td>
    </tr>
    <tr>
      <td>Begin Date</td><td><input type="date" name="begindate" id="bagindate" /><input type="time" name="beginhour"  id="beginhour" /></td>
    </tr>
    <tr>
      <td>End Date</td><td><input type="date" name="enddate" id="enddate" /><input type="time" name="endhour" id="endhour" /></td>
    </tr>
    <tr>
      <td>Local</td><td><input type="text" name="local" id="local" /></td>
    </tr>
    <tr>
      <td>Description</td><td><input type="textarea" rows="10" name="description" id="description" /></td>
    </tr>
    <tr>
     <td><input type="Submit" value="Save" /></td>
    </tr>
  </table>
</form>

<%= @result_message %>

And for the controller I have this code (mostly taken from the gem site I shared previously).

class GoogleCalendarController < ApplicationController

  def create
    send_event(params[:title],params[:begindate] + " " + params[:beginhour], params[:enddate] + " " + params[:endhour], params[:local], params[:description])

    @result_message = 'Event sent successfully'
    render :welcome => index
  end

  private def send_event(title, begin_datetime, end_datetime, local, description)
    require 'googlecalendar'

    google_calendar_service = GData.new
    google_calendar_service.login(Rails.configuration.google_calendar_email, Rails.configuration.google_calendar_password)
    event = { :title     => title,
              :content   => description,
              :where     => local,
              :startTime => begin_datetime,
              :endTime   => end_datetime}
    google_calendar_service.new_event(event)
  end
end

The thing is that when I try to save an event I get the following error.

uninitialized constant GoogleCalendarController::GData

Supposedly GData is a class defined in the googlecalendar gem, but seems to not being recognized as such.

I have gem 'googlecalendar' on my Gemfile, did bundle install and it appears when I do bundle show googlecalendar.

Does anyone know what can be causing this?

Upvotes: 3

Views: 88

Answers (1)

Shiva
Shiva

Reputation: 12514

The documentation is little bit wrong

try this

require 'googlecalendar'
google_calendar_service = Googlecalendar::GData.new

Since you didnot specify the namespace so first Ruby searched it in the ::global namespace and didnot find, so expected it to be GoogleCalendarController::GData


References

Code from the gem's lib/googlecalendar.rb, you can see the namespace is Googlecalendar

$:.unshift(File.dirname(__FILE__)) unless
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

module Googlecalendar
  # List of lib files to include
  FILES = %w{calendar.rb dsl.rb event.rb gcalendar.rb gdata.rb ical.rb net.rb version.rb}
end

# Add all FILES as require
Googlecalendar::FILES.each { |f| require "googlecalendar/#{f}"}

Upvotes: 3

Related Questions