2
votes

I am fairly new to Rails and am having some trouble linking a new html.erb page to my nav tabs.

I created an app using Scaffolding with a projects_controller.rb. I of course have the index.html.erb which displays my projects_path, it also contains a table of all my projects. I have my new, edit, destroy, update, and show. Show contains the specified project of the projects_path. Okay now what I have attempted to do was create (2) new pages using the projects_controller.rb. So I went and added def delinquent and def closed. Both of which mimic the def index, because I want the exact same functionality on these 2 pages. I created (2) new html.erb pages as well with the names delinquent.html.erb and closed.html.erb. I want these pages to act just like the index page but just only show Delinquent and Closed Projects, not all of the projects that are available like I show on the index.html.erb. I have my navbar, navbar-inner, nav, nav pull-right all in place. I have (3) links ready to go, just don't know how to link my newly created pages to the last 2 links:

  <li><%= link_to "Active Projects", projects_path></li>
  <li><%= link_to "Delinquent Projects", '#' %></li> # I want to link this to delinquent.html.erb
  <li><%= link_to "Closed Projects", '#' %></li> # I want to link this to closed.html.erb
3
Also, fyi, you're missing the closing erb tag %> on the first link_to above.Donovan
Routing must be my issue then. I tried adding delinquent_path in place of the '#' and I get undefined local variable or method 'delinquent_path.Twdeveloper
In my routes.rb though all I have is what was generated during my intial build which is resources :projects. Therefore I just thought if did def delinquent in the projects.controller.rb in would take.Twdeveloper
Yeah, I am typing the code into this thread. It is correct though on my dev environment. Thanks though.Twdeveloper

3 Answers

3
votes

In your controller

def index
  @project = Project.all
end

def delinquent
  @projects = Project.delinquent_projects
  render :template => :index
end

def closed
  @projects = Project.closed_projects
  render :template => :index
end

you can also extract the contents of index.html into a partial(except the title), and render the partial in the corresponding views

in config/routes.rb(in the top of the file)

match 'projects/delinquent' => "projects#delinquent", :as => :delinquent_projects
match 'projects/closed' => "projects#closed", :as => :closed_projects

In view

<li><%= link_to "Active Projects", projects_path %></li>
<li><%= link_to "Delinquent Projects", delinquent_projects_path %></li>
<li><%= link_to "Closed Projects", closed_projects_path %></li>
1
votes

rake routes will show you your current routes and their paths. Just add _path or _url to the end.

1
votes

You're on the right path, there's just a bit more you need to do. If you've already added the following controller actions to projects_controller.rb,

def delinquent
end

def closed
end

and you've created the views, all you need to do is define the routes. This is done in the routes.rb folder, within the config directory. Add these two lines to the file and you should be good to go.

get 'delinquent_projects' => 'projects#delinquent', as: 'delinquent_projects'
get 'closed_projects' => 'projects#closed', as: 'closed_projects'

Now, in your view, you'll have access to the following rails helpers

delinquent_projects_path
closed_projects_path

Before you try using them though, run 'rake routes' in your terminal to see that they were correctly created.

Best of luck, and welcome to Rails!