简体   繁体   English

Ruby on Rails-nil:NilClass的未定义方法`pods'

[英]Ruby on Rails - undefined method `pods' for nil:NilClass

I am new to development and new to Rails. 我是开发新手,还是Rails新手。

I have 2 resources Projects and Pods. 我有2个资源Projects和Pod。 //Pods are like sub projects //窗格就像子项目

-----config/routes.rb------ ----- config / routes.rb ------

  resources :projects
  resources :pods

------model/project.rb------ ------ model / project.rb ------

    class Project < ActiveRecord::Base
      attr_accessible :description, :name

      **has_many :pods, dependent: :destroy**

      before_save { |project| project.name = name.upcase }

      validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
      validates :description, presence: true
    end

------model/pod.rb------ ------ model / pod.rb ------

    class Pod < ActiveRecord::Base
      attr_accessible :description, :name

      belongs_to :project


      before_save { |pod| pod.name = name.upcase }

     validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
      validates :description, presence: true
      validates :project_id, presence: true

      default_scope order: 'pods.created_at DESC'
    end

In project view, I have a link to create a pod 在项目视图中,我具有创建窗格的链接

<p> <%= link_to "Create new Pod", new_pod_path(project: @project.id) %></p>

Here I am sending the project id to new_pod_path so that it when the pod is created, it can be associated with this particular project. 在这里,我将项目ID发送到new_pod_path,以便在创建pod时可以将其与该特定项目关联。

------Pod Controller------ ------吊舱控制器------

def new
  @project = Project.find_by_id(params[:project])
  @pod = Pod.new
end

def create
  @pod = @project.pods.build(params[:pod])
    if @pod.save
      flash[:success] = "Congratulaions! You have created a new pod."
      redirect_to @pod
    else
      render 'new'
    end
  end

====================== ======================

When I click on 'Create new pod' link, the form is displayed and in the debug parameters, I see 当我单击“创建新容器”链接时,将显示该表单,并且在调试参数中,我看到

project: '1'
action: new
controller: pods

I fill in the detail and click submit, I get the following error 我填写详细信息,然后单击提交,出现以下错误

NoMethodError in PodsController#create

undefined method `pods' for nil:NilClass

I am not sure what is happening here. 我不确定这里发生了什么。 I do not want to have nested resources as of now because pods are going to have test suites and then test suites are going to have test cases. 我现在不希望嵌套资源,因为Pod将具有测试套件,然后测试套件将具有测试用例。

Any help will be really appreciated 任何帮助将不胜感激

===================================== EDIT ---- ===================================编辑----

I do not know what is going wrong. 我不知道出了什么问题。 Let me put it more clearly. 让我说得更清楚些。 I have a project show page which has new_pod_path with project id ---- " 我有一个项目显示页面,其中的new_pod_path带有项目ID ----“

<%= link_to "Create new Pod", new_pod_path(project_id: @project.id) %> <%=链接到“创建新Pod”,new_pod_path(project_id:@ project.id)%>

" ---------- which goes to new action in the pod controller.. where I have the code as ----- “ ----------转到pod控制器中的新动作..我的代码为-----

  def new @project = Project.find(params[:project_id]) @pod = Pod.new end 

Do I need to find the project in new action at all? 我是否需要以新动作找到该项目?

The the pod view for new : 新的pod视图:

  <%= f.hidden_field :project_id, value: @project.id %> 

//I am guessing from new method, view will be able to see the @project.id and including the above code in new action to find the project makes sense //Please correct me if I am wrong-------- //我正在从新方法中猜测,视图将能够看到@ project.id并将上述代码包含在新操作中以找到项目是有意义的//如果我错了,请纠正我------- --

Then I do the create-------- 然后我做创建--------

  def create @project = Project.find_by_id(params[:project_id]) @pod = @project.pods.build(name: params[:name], description: params[:description]) if @pod.save ... 

I do not know what is going on. 我不知道发生了什么。 If you could tell me the flow for example -- Project show page-- calls the new_pod_path with the project id in arguments. 如果您能告诉我流程,例如“项目显示页面”,则在参数中调用带有项目ID的new_pod_path。 new pod action takes that id and finds the project or somehow makes it available in the new pod form. 新广告连播操作会获取该ID,并找到该项目或以某种方式使其在新广告连播表单中可用。 new pod form uses taht id and passes as a hidden_field.. and then create action takes taht hidden field through params and finds teh projects and creates project.pods.build(... I would really appreciate 新的pod表单使用taht id并将其作为hidden_​​field传递..然后create action通过params将taht隐藏的字段作为参数,并找到项目并创建project.pods.build(...

------------- Server logs -------------Seems like problem with project id only ---------- Started POST "/pods" for 127.0.0.1 at 2013-03-15 17:55:56 -0700 Processing by PodsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"fXCOH0kUgHHVtrH79d4VCrHWJakdGQ8cKQmiJlE0OV4=", "pod"=>{"project_id"=>"1", "name"=>"sdf", "description"=>"df"}, "commit"=>"Create new pod"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '70InmWq61iFCnjzuxhBP6g' LIMIT 1 Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" IS NULL LIMIT 1 Completed 500 Internal Server Error in 4ms -------------服务器日志-------------好像只有项目ID的问题----------已启动POST“ / pods对于2013年7月15日17:55:56 -0700的127.0.0.1,由PodsController#create作为HTML参数进行处理:{“ utf8” =>“✓”,“ authenticity_token” =>“ fXCOH0kUgHHVtrH79d4VCrHWJakdGQ8cKQmiJlE0OV4 =”,“ pod” => {“ project_id” =>“ 1”,“ name” =>“ sdf”,“ description” =>“ df”},“ commit” =>“ Create new pod”}用户负载(0.4ms)选择“ users“。*从” users“位置,” users“。” remember_token“ ='70InmWq61iFCnjzuxhBP6g'LIMIT 1项目负载(0.2ms)选择” projects“。*从” projects“位置,” projects“。” id“是NULL LIMIT 1在4毫秒内完成500个内部服务器错误

NoMethodError (undefined method pods' for nil:NilClass): app/controllers/pods_controller.rb:23:in create' NoMethodError( pods' for nil:NilClass): app/controllers/pods_controller.rb:23:in未定义方法pods' for nil:NilClass): app/controllers/pods_controller.rb:23:in create'

in create you are missing 在创建中,你失踪了

@project = Project.find(params[:project])

Make sure the project_id is in params key :project 确保project_id在params键中:project

On requests the view variables @ need to be recreated. 在请求时,需要重新创建视图变量@

Edit: 编辑:

How are you putting the project_id in the params hash it looks wrong ( project_id"=>{"1"=>""}, ) (to get that id you do params[:project_id].first.key , but that's ugly) 您如何将project_id放在params哈希中,看起来好像是错误的( project_id"=>{"1"=>""}, )(要获取该ID,您可以执行params[:project_id].first.key ,但这很丑)

If you do like codeit suggested, 如果您确实喜欢codeit的建议,

<%= hidden_field 'project', @project.id %> <!--@project initialized in new -->

Or you can do 或者你可以做

<%= hidden_field 'project', params[:project] %> <!--params[:project] coming from the new request -->

Then params[:project] will give you the id 1 然后params[:project]会给你id 1

Use hidden_field_tag in new pod form: new pod格式中使用hidden_field_tag

<%= hidden_field_tag 'project_id', @project.id %>   #Add this line in new pod form

Changes in controller: 控制器的变化:

def create
  @project = Project.find(params[:project_id])
  @pod = @project.pods.build(params[:pod])
  if @pod.save
    flash[:success] = "Congratulaions! You have created a new pod."
    redirect_to @pod
  else
    render 'new'
  end
end

Your instance variable @pod is nil, which means your methods will raise exception. 您的实例变量@pod为nil,这意味着您的方法将引发异常。 You have to fulfill your @pod with something, before you can use methods to it 在使用@pod之前,必须先完成一些事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM