Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading