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.
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:
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
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
On you stack you can read at the bottom
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
- → Trigger a click with jQuery using link_to of rails 4
- → Adding html data attribute to simple_forms input
- → How to remove parameters from the root URL if it does I18n
- → passing parameters to rails back end from an ajax call
- → Blocking ?page= in robots.txt
- → react js and rails Updating state on a component with active record relationship
- → State not passed into prop
- → Cannot read property 'modalIsOpen' of undefined
- → Objects not valid issue
- → How to map API params to model
- → Consuming webhooks shopify-api
- → How to add ScriptTag on shopify_api gem?