0

我正在使用 IDE RubyMine 编写一个基于 Web 的 Ruby 程序。

我有几节课:

application_controller.rb:

class application_controller < ActionController::Base
# To change this template use File | Settings | File Templates.
# require './course_modules.rb'
def initialize
  self.class.main_menu
end

=begin
  def navigateTo(what)
    what.new(v).display
    mainMenu
  end
=end

def self.main_menu
  puts "What would you like to do?
      1: Add module to a scheme
      2: Remove module from a scheme
      3: Query modules
      4: Modify module
      5: Register a student on a scheme
      6: Remove a student from a scheme
      7: Register a student on a module
      8: Remove a student from a module"
  case gets.strip
    when "1"
      CourseModules.add_module
    when "2"
      CourseModules.remove_module
    when "3"
      navigateTo CourseModules
    when "4"
      navigateTo CourseModules
    when "5"
      navigateTo Student
    when "6"
      navigateTo Student
    when "7"
      navigateTo Student
  end
end
Application.new
end

course_modules.rb:

class CourseModulesController < ActionController::Base
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
@noOfModulesInScheme = 0

$schemes = {}
$module_ID = 0
$module_exists = false

def self.moduleYear
  @@moduleYear
end

def initialize(v)
  @val = v
end
# Set and get the @val object value
def set (v)
  @val = v
end
def get
  return @val
end


# Attempt at add_module method on 21/08/2012 at 16:30
def self.add_module
  # schemes = {}
  scheme_exists = false
  add_another_scheme = true
  # module_exists = false
  add_another_module = true

  while add_another_scheme
    print "Enter scheme name: "
    scheme_name = gets
    $schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

    if !scheme_exists
      $schemes[scheme_name.chop] = []
      puts "Scheme #{scheme_name.chop} has been added to the system"
    elsif
    scheme_exists == true
      puts "This scheme has already been added"
    end

    while add_another_module
      print "Enter module name: "
      module_name = gets
      $schemes[scheme_name.chop].include?(module_name.chop) ? true : $schemes[scheme_name.chop] << module_name.chop
      # puts "Module #{module_name.chop} has been added to #{scheme_name.chop}"

      # 22/08/2012 at 14:15 Now need to read in each module's unique identifier and year it belongs to
      print "Enter module ID: "
      $module_ID =gets
      $schemes[scheme_name.chop].include?($module_ID.chop) ? true : $schemes[scheme_name.chop] << $module_ID.chop
      $schemes.has_key?($module_ID.chop) ? module_exists = true : module_exists = false

      print "Enter the academic year to which the module belongs: "
      module_year = gets
      $schemes[scheme_name.chop].include?(module_year.chop) ? true : $schemes[scheme_name.chop] << module_year.chop

      if !$module_exists
        $schemes[$module_ID.chop] = []
        puts "Module #{$module_ID.chop} : #{module_name.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
      elsif
      $module_exists == true
        puts "A module with this ID has already been added to the scheme, please check if the module already exists, or choose another ID "
      else
        #  puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
      end

      # puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop}"

      print "Add another module? "
      ask_if_user_wants_to_add_another_module = gets
      if(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module == "yes")
        add_another_scheme = false
      else if(ask_if_user_wants_to_add_another_module.chop != "y" or ask_if_user_wants_to_add_another_module != "yes")
             Application.main_menu
           end
      end

    end

    print "Add another scheme? "
    ask_if_user_wants_to_add_another_scheme = gets
    if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
      add_another_scheme = false
    end
    puts $schemes

  end

  while add_another_module
    print "Enter scheme name: "
    scheme_name = gets
    #$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
    scheme_exists = $schemes.has_key? (scheme_name.chop)

    if !scheme_exists
      print "Enter module name: "
      module_name = gets
      $schemes[scheme_name.chop] = module_name.chop
      puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
    else
      scheme_exists = false
      puts "This scheme has already been added"
      puts "Enter module name: "
      module_name = gets
      $schemes[scheme_name.chop] = module_name.chop
      puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
    end

    print "Add another module? "
    ask_if_user_wants_to_add_another_module = gets
    if !(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module.chop == "yes")
      add_another_module = false
    end
  end


  puts $schemes
end
def self.remove_module

  print "Which scheme would you like to remove a module from? "
  scheme_name = gets
  #$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
  scheme_exists = $schemes.has_key? (scheme_name.chop)

  if !scheme_exists
    $schemes[scheme_name.chop] = []
    puts "Scheme #{scheme_name.chop} doesn't exist"
  else
    scheme_exists = true
    puts "Which module would you like to remove from #{scheme_name.chop}?"
    $module_ID = gets
    if !$module_exists
      $schemes[$module_ID.chop] = []
      puts "Module #{$module_ID.chop} : does not exist in #{scheme_name.chop} "
    else
      module_exists = true
      puts "Module #{$module_ID.chop} has been removed from #{scheme_name.chop} "
      #  puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
    end
  end

 end

end

当我浏览到

http://localhost:3000 

在 Firefox 中,要查看我的网页,我会得到一个显示错误消息的网页:

错误消息说:

SyntaxError in ApplicationController#main_menu

(Filepath)/application_controller.rb:1: class/module name must be CONSTANT
class application_controller < ActionController::Base

Rails.root: (Filepath)/CourseManagementSystem

有谁知道为什么我没有显示我的 Web 应用程序的网页?我怎样才能把这个正确?

4

1 回答 1

0
class application_controller < ActionController::Base

这应该是

class ApplicationController < ActionController::Base

application_controller是 ruby​​ 中的无效类名。您可以通过运行在 IRB 中简单地测试它

➜  ~  irb
irb(main):001:0> class a;end
SyntaxError: (irb):1: class/module name must be CONSTANT
class a;end
    ^
    from /Users/deefour/.rbenv/versions/1.9.3-p125/bin/irb:12:in `<main>'

使用正确命名的类A而不是a,就不存在这样的问题

➜  ~  irb
irb(main):001:0> class A;end
=> nil
irb(main):002:0> 
于 2012-08-25T20:05:49.453 回答