0

I created a new controller "user" with the actions register and index. However when I go on my browser to http://localhost:3000/user/register i get an error. It tells me No route matches {:action=>"about", :controller=>"user"}

Did I not create the controller correctly?

Here's some of my code.

Routes.rb

Rails.application.routes.draw do
  get 'user/index'

  get 'user/register'

  get 'site/index'

  get 'site/about'

  get 'site/help'

  root 'site#index'

User Controller

class UserController < ApplicationController
  def index
  end

  def register
    @title = 'Register'
  end
end

UPDATE it keeps telling me there is a problem with the line between the ***

<!DOCTYPE html>
<html>
<head>
  <title><%= @title %></title>
  <%= stylesheet_link_tag 'application.css' %>
</head>
<body>
<div id="whole_page">
  <div id="header">Work<b>Link</b></div>
  <div id= "nav">
    <%= link_to_unless_current'Home', action: 'index' %> |
    ***<%= link_to_unless_current'About', action: 'about' %> |***
    <%= link_to_unless_current'Help', action: 'help' %>
  </div>
  <div id="content">
    <%= yield %>
    <%= @content %>
  </div>
</div>
</body>
</html>
Corey Holmes
  • 368
  • 3
  • 13

2 Answers2

1

I would switch your routes/controllers to the following. Also, keep your controller names plural.

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
  end

  def register
  end
end

# config/routes.rb
resources :users, only: %w(index) do
  get :register, on: :collection
end

I specified on: :collection, because the route doesn't belong to a member of the collection. ie: it doesn't need an object's ID, like users/1/ would. The other option is member, which of course is a route that belongs to a member of the collection.

Now, visit localhost:3000/users/register.

Community
  • 1
  • 1
Justin
  • 4,922
  • 2
  • 27
  • 69
0

your routes.rb file should also have plural naming. This answer supports Justin's answer.

so this line for example:

user/register

should be

users/register

you might end up in get errors which you have to fix but if my suggestion works. then you are good to go.

Based on your update do you have an about method defined???

mrtorks
  • 66
  • 2
  • 11