Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails - undefined method `pods' for nil:NilClass
    text
    copied!<p>I am new to development and new to Rails. </p> <p>I have 2 resources Projects and Pods. //Pods are like sub projects </p> <p>-----config/routes.rb------</p> <pre><code> resources :projects resources :pods </code></pre> <p>------model/project.rb------</p> <pre><code> class Project &lt; 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 </code></pre> <p>------model/pod.rb------</p> <pre><code> class Pod &lt; 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 </code></pre> <hr> <p>In project view, I have a link to create a pod</p> <pre><code>&lt;p&gt; &lt;%= link_to "Create new Pod", new_pod_path(project: @project.id) %&gt;&lt;/p&gt; </code></pre> <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.</p> <p>------Pod Controller------</p> <pre><code>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 </code></pre> <p>======================</p> <p>When I click on 'Create new pod' link, the form is displayed and in the debug parameters, I see</p> <pre><code>project: '1' action: new controller: pods </code></pre> <p>I fill in the detail and click submit, I get the following error</p> <pre><code>NoMethodError in PodsController#create undefined method `pods' for nil:NilClass </code></pre> <p>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.</p> <p>Any help will be really appreciated</p> <p>===================================== EDIT ----</p> <p>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 ---- "<p> &lt;%= link_to "Create new Pod", new_pod_path(project_id: @project.id) %></p>" ---------- which goes to new action in the pod controller.. where I have the code as -----</p> <pre><code> def new @project = Project.find(params[:project_id]) @pod = Pod.new end </code></pre> <p>Do I need to find the project in new action at all?</p> <p>The the pod view for new :</p> <pre><code> &lt;%= f.hidden_field :project_id, value: @project.id %&gt; </code></pre> <p>//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--------</p> <p>Then I do the create--------</p> <pre><code> def create @project = Project.find_by_id(params[:project_id]) @pod = @project.pods.build(name: params[:name], description: params[:description]) if @pod.save ... </code></pre> <p>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. new pod action takes that id and finds the project or somehow makes it available in the new pod form. 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 </p> <p>------------- 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</p> <p>NoMethodError (undefined method <code>pods' for nil:NilClass): app/controllers/pods_controller.rb:23:in</code>create'</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload