轨道“跟随”关系控制器问题
我正在开发一个应用程序,用户的“项目”可以跟踪数据库中的“植物”对象。当我在应用程序中为任意数量的植物对象点击“关注”时,我的“Prelationships”控制器(将用户的项目连接到固定“植物”对象的植物关系)中的创建操作出现以下错误:
“当你没有预料到的时候,你就得到了一个零对象! 您可能期望一个 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
最后,我的“Prelationships ”模型:视图的“_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)
I am working on an app where users' "projects" can follow "plant" objects from the database. I am getting the following error for the create action in my "Prelationships" controller (Plant Relationships that connect Users' projects to fixed "plant" objects) when I hit "Follow" for any number of plant objects in my app:
"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.[]"
I realize this is a big question, and yes, I'm pretty much a newbie. All the migrations should be fine. I appreciate any help--even if it means suggesting a whole new way of tackling this issue.
Here's what my controller, called "Prelationships", looks like:
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
And my "Prelationships" model:
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
And my "Projects" model:
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
And my "plant" model:
class Plant < ActiveRecord::Base
has_many :prelationships, :foreign_key => "pfollowed_id",
:class_name => "Prelationship"
has_many :pfollowers, :through => :reverse_prelationships,
:source => :pfollower
end
And, finally, my "_plants_form" partial for the view:
<%= 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 %>
Here's the error from my log:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。就像 Swanand 指出的那样,您应该在
:create action
中初始化@project
对象。如果您不使用before_filter
执行此操作,则类似于@project = Project.find(params[:project_id])
。如果您之前已经实例化过
@project
,请查看当您在 Rails 控制台Plant.find(1)
中手动尝试检索@plant
对象时会发生什么Yep. Like Swanand point out, you should initialize
@project
object inside:create action
. Like@project = Project.find(params[:project_id])
if you do not do it with abefore_filter
.If you already instantiated
@project
before, see what happens when you manually try to retrieve@plant
object in Rails consolePlant.find(1)