使用Mongo Mapper的Rails 3

时间:2012-03-12 06:26:45

标签: ruby-on-rails-3 mongomapper

我正在使用MongoDB(MongoMapper)在rails 3中开发1个应用程序,其中我需要关联三个类,称为用户,空间和地址,并且想要相应地获取数据但是我收到此错误,我是MongoMapper的新手,不要知道如何解决它,PLZ帮助我!!!

   NoMethodError in Space#index

    Showing C:/Users/bacancy1/Documents/Aptana Studio 3 Workspace/Jigar/awesome_app/app/views/space/index.html.erb where line #9 raised:

    undefined method `street' for nil:NilClass

相关模型

1> address.rb

 class Address
    include MongoMapper::Document
    validates_presence_of :street, :city, :state, :zip, :neighborhood, :country_id
    key :street, String
    key :city, String
    key :state, String
    key :zip, String
    key :neighborhood, String
    key :country_id, String
    belongs_to :user , :class_name =>'User'
    belongs_to :space , :class_name =>'Spaces'
    end

2 - ; space.rb

   class Space
  include MongoMapper::Document

  validates_presence_of :name, :occupancy, :additional_services, :start_time, :end_time

  key :name, String
  key :is_indoor, Boolean
  key :is_outdoor, Boolean
  key :photo, Binary
  key :summary, String
  key :occupancy, String
  key :additional_services, String
  key :start_time, String
  key :end_time, String
  key :status, Integer
  key :created_on, DateTime
  key :address_id, String
  key :user_id, String


   one :address , :class_name =>'Address'
   belongs_to :user

3> user.rb

    class User
    include MongoMapper::Document         
  #plugin MongoMapper::Devise

  validates_presence_of :firstname, :lastname, :email, :password
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
  validates_uniqueness_of :email
  validates_confirmation_of :password

  #key :_id, String
  key :firstname, String
  key :lastname, String
  key :email, String
  key :password, String
  key :confirm, String
  key :is_admin, Boolean

  one :address , :in => :addresses, :class_name =>'Address'
  validates_associated :addresses



  many :spaces, :in => :spaces, :class_name => 'Space'
  validates_associated :spaces

  #devise :database_authenticatable, :confirmable, :lockable, 
  #       :recoverable, :rememberable, :registerable, :trackable, 
  #       :timeoutable, :validatable, :token_authenticatable

  #attr_accessible :email, :password, :password_confirmation

end

    #devise :database_authenticatable, :confirmable, :lockable, 
  #       :recoverable, :rememberable, :registerable, :trackable, 
  #       :timeoutable, :validatable, :token_authenticatable

  #attr_accessible :email, :password, :password_confirmation

end

4> index.html.erb(我希望通过其查看数据的视图文件)

 <!-- detail part -->
<div class="main_wrapper_box">
    <div class="sub_detail_box">
        <div class="title_box">
            <div class="title_text">
                <%= @space.name %>
            </div>
            <div class="title_sub_text">
              <h2> <%= @space.address.street %>,<%= @space.address.city %>,<%= @space.address.state %>,<%= @space.address.zip %></h2>
            </div>
        </div>
        <!-- start detail -->
        <div class="detail_box">
            <!-- 1 location -->
            <div class="comman_sub_detail_box">
                <!-- left side -->
                <div class="fea_lan_leftside">
                    <!-- start tab control -->
                    <div class="container">
                        <ul class="menu">
                            <li id="activityfeed" class="active">
                                Overview
                            </li>
                            <li id="verify">
                                Highlights
                            </li>
                            <li id="claims">
                                Map
                            </li>
                            <li id="confirm">
                                Albums
                            </li>
                        </ul>.....(continue.... )

1 个答案:

答案 0 :(得分:0)

您的address.rb

中有拼写错误
    belongs_to :space , :class_name =>'Spaces'

您没有名为Spaces的课程。你有一个名为Space的班级。它应该是:

    belongs_to :space , :class_name =>'Space'

这可能会或可能不会解决您的问题。