nil的未定义方法`first_name':安装设备后的NilClass

时间:2012-05-21 03:00:03

标签: ruby-on-rails devise routes associations

当我尝试运行sunspot:reindex或在属于用户的Gears的视图中时,我继续从数据库的2列first_name和last_name获取未定义的方法。在安装设备之前它工作正常。请参阅下面的代码。谢谢你的帮助。

用户模型

class User < ActiveRecord::Base 
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :userimage, :remove_userimage
  has_many :gears
  has_many :comments, :dependent => :destroy 
  has_one :store, :dependent => :destroy
  require 'carrierwave/orm/activerecord'
  mount_uploader :userimage, UserpicUploader
  accepts_nested_attributes_for :store

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :first_name,  presence: true,
                          length:  {:maximum => 50 }
  validates :last_name,  presence: true,
                         length:  {:maximum => 50 }                                         

  def name
   first_name + " " + last_name
  end

齿轮模型

class Gear < ActiveRecord::Base
  attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url, :color, :year, :latefee, :cancellation, :minrental, :policy, :about, :address, :city, :state, :zip
  belongs_to :user
  belongs_to :sub_category
  has_one :category, :through => :sub_category
  has_many :comments, :dependent => :destroy 
  require 'carrierwave/orm/activerecord'
  mount_uploader :image, GearpicUploader
  mount_uploader :image_a, GearpicUploader

  validates :title, presence: true
  validates :size,  presence: true
  validates :price, presence: true
  validates :sub_category_id, presence: true
  validates :user_id, presence: true

  searchable do
     text :title, :size, :price, :year, :zip, :state, :city, :minrental, :about, :latefee, :color 

     text :user_first_name do
          user.first_name
     end

     text :user_last_name do
          user.last_name
     end
     # **Facet Section**   

     string :size, :price, :year, :zip, :state, :city, :minrental, :latefee, :color 

     string :sub_category_name do
       sub_category.name
     end

     string :category_name do
       category.name
     end
   end

end

Gear Controller Index&amp;显示

class GearsController < ApplicationController
  def index
    @search = Gear.solr_search do
            fulltext params[:search]
            paginate(page: params[:page])
            facet (:category_name)
            facet (:sub_category_name)
            with(:sub_category_name, params[:name]) if params[:name].present?
            with(:category_name, params[:categoryname]) if params[:categoryname].present?
    end

    @gears = @search.results
  end

  def show
    @gear = Gear.find(params[:id])
    @storegear = @gear.user.gears.all(:order => 'created_at DESC', :limit => 6)
    @comments = @gear.comments.all
    @user = @gear.user
    @store = @gear.user.store.id
  end

观看我试图展示

<%= link_to gear_path(gear) do %>
<div class="gear_list_box">
    <div class="gear_list_box_top"><%= image_tag gear.image_url(:list), class: 'gear_list_box_top_pic' %></div>
    <div class="gear_list_box_bottom">
        <table style="border-collapse:collapse; cellspacing: 0; width: 100%; margin: 0;" >
            <tr>
                <td style="color: #2c6aa7; padding-left: 10px" width="60%"> <span class='gearlist_size'><%= gear.size %></span> - <%=  truncate(gear.title,:length => 12) %></td>
                <td rowspan="2" width="40%" style="color: #5c8d0d; text-align: center; vertical-align: middle; font-size: 120%; border-collapse:collapse;">$<%= gear.price %>/day</td>
            </tr>
            <tr>
                <td width="60%" style="padding-left: 10px; font-size: 12px;"><%= gear.user.first_name + " " + gear.user.last_name %></td>
            </tr>
        </table>
    </div>
</div>
<% end %>

路线

  devise_for :users, :path => 'accounts', :controllers => {:registrations => "registrations"}

  devise_scope :user do
    get "signin", :to => "devise/sessions#new"
  end

  devise_scope :user do
    post "sign_in", :to => "devise/sessions#create"
  end

  devise_scope :user do
    delete "signout", :to => "devise/sessions#destroy"
  end

  devise_scope :user do
    get "signup", :to => "devise/registrations#new"
  end

  resources :gears do
    resources :comments, :only => [:create, :destroy] 
  end

  match '/gear',   to: "gears#index"

  resources :users, :except => [:destroy, :create, :new ] do
     resources :store
  end

  root to: 'pages#home'

  match '/about',   to: 'pages#about'
  match '/contact', to: 'pages#contact'
  match '/fees',    to: 'pages#fees'
  match '/faq',     to: 'pages#faq' 
  match '/why',     to: 'pages#why'  
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy'

错误 *

rake sunspot:reindex --trace
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sunspot:reindex
rake aborted!
undefined method `first_name' for nil:NilClass
/Users/dave/rails_projects/EquiptMe/app/models/gear.rb:21:in `block (2 levels) in <class:Gear>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/util.rb:208:in `instance_eval'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/util.rb:208:in `instance_eval_with_context'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/util.rb:86:in `instance_eval_or_call'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/data_extractor.rb:33:in `value_for'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/field_factory.rb:58:in `populate_document'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:95:in `block in prepare'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:94:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:94:in `prepare'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:24:in `block in add'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:24:in `map'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/indexer.rb:24:in `add'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/session.rb:91:in `index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot.rb:184:in `index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/searchable.rb:256:in `block (2 levels) in solr_index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/searchable.rb:345:in `solr_benchmark'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/searchable.rb:255:in `block in solr_index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/querying.rb:8:in `find_in_batches'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/searchable.rb:254:in `solr_index'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/searchable.rb:198:in `solr_reindex'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/tasks.rb:54:in `block (3 levels) in <top (required)>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/class_set.rb:16:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot-1.3.1/lib/sunspot/class_set.rb:16:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/sunspot_rails-1.3.1/lib/sunspot/rails/tasks.rb:53:in `block (2 levels) in <top (required)>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `load'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `<main>'
Tasks: TOP => sunspot:reindex
Daves-MacBook-Pro:EquiptMe dave$ 

3 个答案:

答案 0 :(得分:1)

可搜索是一个块,因此您可以重写类似的内容:

searchable do
     text :title, :size # the rest...

     text :user_first_name do
          user.nil?? '' : (user.first_name.nil?? '' : user.first_name)
     end
end

为我工作。

答案 1 :(得分:0)

您是否尝试过通过齿轮模型引用first_namelast_name,而不是通过设计提供的current_user参考来访问它们?

例如,更改视图以使用此行可能会有所帮助:

current_user.first_name + " " + current_user.last_name

一旦开始使用设计,访问当前用户的最佳方式是通过current_user便捷方法。

答案 2 :(得分:0)

我已经修好了。问题是我的样本数据中没有用户的Gear对象。删除齿轮后,它工作正常。