Ruby on rails 铁路";跟随;关系控制器问题
我正在开发一个应用程序,用户的“项目”可以跟踪数据库中的“植物”对象。当我在我的应用程序中点击任意数量的Plant对象的“Follow”时,我在我的“Preliationships”控制器(将用户的项目连接到固定的“Plant”对象的Plant Relationships)中的创建操作出现以下错误: “当你没有预料到的时候,你有一个nil对象! 您可能期望数组的一个实例。 计算nil时出错。[]“ 我意识到这是个大问题,是的,我几乎是个新手。所有迁移都应该是正常的。我感谢任何帮助——即使这意味着提出一种全新的解决这个问题的方法 下面是我的控制器,称为“Preliationships”,看起来是这样的:Ruby on rails 铁路";跟随;关系控制器问题,ruby-on-rails,ruby-on-rails-3,rails-activerecord,relationship,Ruby On Rails,Ruby On Rails 3,Rails Activerecord,Relationship,我正在开发一个应用程序,用户的“项目”可以跟踪数据库中的“植物”对象。当我在我的应用程序中点击任意数量的Plant对象的“Follow”时,我在我的“Preliationships”控制器(将用户的项目连接到固定的“Plant”对象的Plant Relationships)中的创建操作出现以下错误: “当你没有预料到的时候,你有一个nil对象! 您可能期望数组的一个实例。 计算nil时出错。[]“ 我意识到这是个大问题,是的,我几乎是个新手。所有迁移都应该是正常的。我感谢任何帮助——即使这意味着
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
是的。正如Swand指出的,您应该初始化
@project
中的:create action
对象。如果不使用过滤器前的,则类似于@project=project.find(params[:project\u id])
如果您之前已经实例化了@project
,请查看在Rails控制台plant中手动检索@plant
对象时会发生什么。查找(1)
您可以粘贴错误的回溯吗?如果您需要其他帮助来破解此问题,请告诉我正在实例化@project
的位置在哪里?啊,是的!非常感谢。一个很大的疏忽。这就解决了这个问题。。。尽管在:create action
中插入该代码片段后我仍然存在问题,但在提交\u plants\u表单
部分后,我现在收到一条找不到没有ID的项目的错误消息。我猜在我的RESTful操作中有一些脱节,尽管我很感激任何关于我犯了什么错误的离别想法。检查嵌套路由。他们可能会帮你很多忙。阅读/观看特别是Ryan对它的看法。(铁路广播#196+#197)。您可能还希望检查继承的资源和简单表单gem。享受编码。
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
<%= 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)