Rails 3 с Mongo Mapper - PullRequest
       9

Rails 3 с Mongo Mapper

0 голосов
/ 12 марта 2012

Я разрабатываю 1 приложение в рельсах 3 с MongoDB (MongoMapper), в котором мне нужно связать три класса, называемых user, space и address, и хочу получить данные соответственно, но я получаю эту ошибку, я новичок в MongoMapper, Dont знаете, как решить эту проблему, помогите мне, пожалуйста!

   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 голосов
/ 12 марта 2012

У вас есть опечатка в вашем address.rb:

    belongs_to :space , :class_name =>'Spaces'

У вас нет класса с именем Spaces. У вас есть класс с именем Space. Должно быть:

    belongs_to :space , :class_name =>'Space'

Это может или не может решить вашу проблему.

...