0
votes

Is my project class is nill or what, can any one explain me this error.

NoMethodError (undefined method projects' for nil:NilClass):
app/controllers/project_controller.rb:8:in
index'

Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.2ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.4ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (50.0ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (41.3ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (93.4ms)

Here is my project controller

before_action :confirm_logged_in

  before_action :find_company


  def index

    @projects = @company.projects.sorted
  end

  def show

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

  def new

    @project = Project.new()
  end

  def create

    @project = Project.new(project_params)
    if @project.save
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  def edit

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

  def update

    @project = Project.find(params[:id])
    if @project.update_attributes(project_params)
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  def delete

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

  def destory

    @project = Project.find(params[:id])
    @project.destroy
    redirect_to(:action => 'index')
  end


  private 

  def project_params

   params.require(:project).permit(:name, :position, :type_of_project, :description, :no_of_tasks)
  end

  def find_company

    if params[:company_id]
      @company = Company.find(params[:company_id])
    end
  end
end

So actually what iam doing here is before entering in to projects we need to find the company_id and in index @projects = @company.projects.sorted means that company has many projects.

1
Can you please share your project.rb and company.rbDusht
class Project < ActiveRecord::Base belongs_to :company has_many :tasks has_and_belongs_to_many :users scope :sorted, lambda{order("position ASC")} scope :newest_first, lambda{order("created_at ASC")} enduser5575586
class Company < ActiveRecord::Base has_many :projects scope :sorted, lambda{order("position ASC")} scope :newest_first, lambda{order("created_at ASC")} enduser5575586
What is @company in the index method? Is Company related to Project with a has_many?bo-oz
Yeah, company has has_many projects and I also did page nestinguser5575586

1 Answers

1
votes

The problem is with the if params in the def find_company. For the index function this is not set.

change def index to:

def index
  @projects = Project.sorted
end

UPDATE: made an error I think:

def index
  @projects = Project.all.sorted #or leave the call to sorted out completely
end