Ruby on Rails类被误认为是常量。

3
首先,我第一次尝试使用Ruby on Rails。 我正在进行一个简单的练习,设计一个表单来接收事件信息,并将这些事件保存在Google日历中。 为了将事件保存在Google日历中,我正在使用以下gem:http://googlecalendar.rubyforge.org/ 下面是视图的代码。
<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 %>

这是我的控制器代码(主要参考我之前分享的宝石网站上的代码)。

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

问题在于,当我尝试保存事件时,会出现以下错误。
uninitialized constant GoogleCalendarController::GData

据说GData是googlecalendar gem中定义的一个类,但似乎没有被识别为这样的类。
我在我的Gemfile文件中添加了'gem'googlecalendar',并执行了'bundle install',当我执行'bundle show googlecalendar'时,它也出现了。
有人知道是什么原因造成的吗?

也许你需要在控制器类的顶部添加 require 'googlecalendar'? - mockaroodev
如果在根命名空间中定义了 ::GData.new,请尝试使用它。 - xiaoboa
尝试将require 'googlecalendar'放在类定义的下方顶部。 - Pavan
实际上,我已经尝试过在控制器的顶部和类定义后面直接使用 require 'googlecalendar',但它没有起作用。即使使用 ::GData.new 也不行,有没有办法可以检查类命名空间? - Andre
@Andre:不存在所谓的“类命名空间”。类和其他对象一样,它们可以从方法中返回,可以作为方法的参数传递,可以赋值给变量。通常情况下,它们被赋值为常量。 - Jörg W Mittag
1个回答

3

这份文档有一点错误

尝试这个

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

由于您没有指定命名空间,所以Ruby首先在全局命名空间中搜索它,但没有找到,因此期望它是GoogleCalendarController::GData


参考资料

来自gem的lib/googlecalendar.rb代码,您可以看到该命名空间为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}"}

谢谢,就这些了。之前我实际上尝试过GoogleCalendar :: GData.new(大写'C'),但它没有起作用。现在我遇到了一个与SSL相关的错误,但至少这个问题已经解决了。这是新错误。 SSL_connect returned = 1 errno = 0 state = SSLv3 read server certificate B:certificate verify failed我正在考虑放弃这个gem并尝试更好的替代方案。 - Andre
是的,我同意你的想法;尝试寻找更简单的解决方案,例如 https://github.com/northworld/google_calendar。 - Shiva
谢谢,我还没有找到那个。 - Andre

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接