Rails“跟随”关系控制器问题

时间:2011-11-20 21:55:43

标签: ruby-on-rails ruby-on-rails-3 rails-activerecord relationship

我正在开发一个应用程序,用户的“项目”可以跟踪数据库中的“工厂”对象。我在我的“预关系”控制器(将用户的项目连接到固定的“工厂”对象的工厂关系)中的创建操作收到以下错误,当我在我的应用程序中点击“关注”任意数量的工厂对象时:

“当你没想到它时,你有一个零对象! 您可能期望一个Array实例。 在评估nil。[]“

时发生错误

我意识到这是一个很大的问题,是的,我几乎是一个新手。所有迁移都应该没问题。我感谢任何帮助 - 即使这意味着建议采用一种全新的方式解决这个问题。

这就是我的控制器,称为“Prelationships”,如下所示:

class PrelationshipsController < ApplicationController  
 def create                                                    
  @plant = Plant.find(params[:prelationship][:pfollowed_id])
  @project.follow!(@plant)
  respond_to do |format|
    format.html { redirect_to @project }
    format.js
  end
 end  
end        

我的“Prelationships”模式:

class Prelationship < ActiveRecord::Base                          
 attr_accessible :pfollowed_id

   belongs_to :pfollower, :class_name => "Project"
   belongs_to :pfollowed, :class_name => "Plant"

   validates :pfollower_id, :presence => true
   validates :pfollowed_id, :presence => true
end

我的“项目”模型:

class Project < ActiveRecord::Base                    
attr_accessible :title, :address, :latitude, :longitude, :state      

 belongs_to :user        
 has_many :prelationships, :foreign_key => "pfollower_id",
                        :dependent => :destroy   
 has_many :pfollowing, :through => :prelationships, :source => :pfollowed  

 def pfollowing?(pfollowed)
   prelationships.find_by_pfollowed_id(pfollowed)  
 end  

 def pfollow!(pfollowed)
  prelationships.create!(:pfollowed_id => pfollowed.id)
 end
end

我的“植物”模型:

class Plant < ActiveRecord::Base      

 has_many :prelationships, :foreign_key => "pfollowed_id",
                        :class_name => "Prelationship"
 has_many :pfollowers, :through => :reverse_prelationships, 
                        :source => :pfollower   
end

最后,我的“_plants_form”部分为视图:

<%= form_for @project.prelationships.build(:pfollowed_id =>
                                           @project_id) do |f| %>
  <%= collection_select(:prelationships, :pfollowed_id, Plant.all, :id, :name, 
  options = {:prompt => "Select your plants"}, :class => "listselect") %>  
  <div class="actions"><%= f.submit "Pfollow" %></div>
<% end %>

以下是我日志中的错误:

 Started POST "/prelationships" for 127.0.0.1 at 2011-11-20 23:31:57 +0100
 Processing by PrelationshipsController#create as HTML
 Parameters: {"utf8"=>"✓",       
 "authenticity_token"=>"NKqa1f0M2yPLQDHbRLnxl3SiwBeTus/1q1hpZjD7hgY=",      
 "prelationships"=>{"pfollowed_id"=>"5"}, "commit"=>"Pfollow"}
 Completed 500 Internal Server Error in 14ms

 NoMethodError (You have a nil object when you didn't expect it!
 You might have expected an instance of Array.
 The error occurred while evaluating nil.[]):
 app/controllers/prelationships_controller.rb:4:in `create'

 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-  
 3.0.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-   
 3.0.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb    
 (30.6ms)
 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack- 
 3.0.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within 
 rescues/layout (37.1ms)

1 个答案:

答案 0 :(得分:1)

是的。像Swanand指出的那样,你应该在@project内初始化:create action对象。与@project = Project.find(params[:project_id])一样,如果您不使用before_filter

如果您之前已经实例化@project,请查看手动尝试在Rails控制台@plant中检索Plant.find(1)对象时会发生什么