Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
ROR: NoMethodError in Home#index

I'm learning Ruby on Rails and as part of practise I'm building a Bug Tracker System, which has a Manager, Developer and QA.

I want to display all the projects the Manager has created on his Home Screen. These proejcts are present in a table projects.

Projects

In my manager_controller, I have this code:

class ManagerController < ApplicationController
  def index
    @projects = Project.all
  end

  def create
  end
end

And this is my index.html.erb in views/manager:

<div class="container">
  <h1>Manager's Feed</h1>
  <p>Hello <%=current_user.username%></p>

  <%= link_to "Create Project", new_project_path, :class => "btn btn-success"%>


  <table id='employee-table' class="table table-hover">
    <thead  class="thead-light">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th colspan="2"></th>
      </tr>
    </thead>

    <tbody>
      <% @projects.each do |project| %>
        <tr>
          <td><%= project.id %></td>
          <td><%= project.title %></td>
        </tr>
      <% end %>
    </tbody>
  </table>


  <div>
    <%= link_to "Logout", destroy_user_session_path, :method => :delete %>
  </div>
</div>

But this is giving me following error:

Error

I'm unable to see why @projects is Null as I had defined it in my controller. Can anyone help me in fixing this problem?

Update 1 My routes.rb file:

Rails.application.routes.draw do
  get 'project/show'
  resources 'project'
  resources 'manager'
  resources 'developer'
  resources 'qa'

  devise_for :users
  get 'home/index'
  root to: "home#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

HomeController:

class HomeController < ApplicationController
  before_action :authenticate_user!

  def index
    @projects = Project.all
    if current_user.manager?
      render '../views/manager/index.html.erb'
    end
    if current_user.developer?
      render '../views/developer/index.html.erb'
    end
    if current_user.quality_assurance?
      render '../views/qa/index.html.erb'
    end
  end
end
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

On you stack you can read at the bottom app/controllers/home_controller.rb

You're initialising @projects on managers_controller.rb.

Use this on the correct controller.

class HomeController < ApplicationController
  def index
    @projects = Project.all
  end
end

Your default route is defined by root to: "home#index" with leads to the HomeController#index. Over there, you're using render which simply "display" the content. I would suggest to use redirect_to to redirect the flow to the correct controller with something like:

class HomeController < ApplicationController
  before_action :authenticate_user!

  def index
    @projects = Project.all
    if current_user.manager?
      redirect_to managers_path
    end
    if current_user.developer?
      redirect_to developers_path
    end
    if current_user.quality_assurance?
      redirect_to qas_path
    end
  end
end

You can find all *_paths values I used running rails routes.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!