0
votes

I am new to development and new to Rails.

I have 2 resources Projects and Pods. //Pods are like sub projects

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

  resources :projects
  resources :pods

------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------

    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.

------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.

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 ---- "

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

" ---------- which goes to new action in the pod controller.. where I have the code as -----
  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 :

    <%= 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--------

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. 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

------------- 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

NoMethodError (undefined method pods' for nil:NilClass): app/controllers/pods_controller.rb:23:increate'

3

3 Answers

1
votes

in create you are missing

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

Make sure the project_id is in params key :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)

If you do like codeit suggested,

<%= 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

0
votes

Your instance variable @pod is nil, which means your methods will raise exception. You have to fulfill your @pod with something, before you can use methods to it

0
votes

Use hidden_field_tag in new pod form:

<%= 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