Home > database >  ROR: NoMethodError in Home#index
ROR: NoMethodError in Home#index

Time:02-11

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

CodePudding user response:

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.

  • Related