'undefined method `users_path'

<%= form_for @user1 do |f| %>

    Username: <%= f.text_field :username %>      <br />
    Password: <%= f.password_field :password %>  <br />.
    Email:    <%= f.email_field :email %> <br />

    <%= submit_tag "Add" %>

<% end %>

I am new on ruby and I am trying create simple programs, but I have this error and I can't find out why. I have this error

undefined method `users_path' for #<#<Class:0x000000020576f0>:0x0000000335d5d8>

what is the solution here?

class UserController < ApplicationController
  def add
    @user1 = User.new
    respond_to do |format|
      format.html
      format.json {render :json => @user1}
    end
  end


Solution 1:[1]

Did you define the routes in config/routes.rb?

Rails.application.routes.draw do
  resources :users
  # ...
end

Solution 2:[2]

I got this error when in my config/routes.rb, I mistakenly had user (singular):

resources :user

It needs to be users (plural):

resources :users

Solution 3:[3]

If you run the rake routes command, you will find that there is no entry for users_path in the very first column. What that means is that you have no route defined for action that will be called when the form is submitted.

If you want to use resources, then just add:

resources: users

and if you want to use a different URL(route name) for the users#new action, then add

resources: users, except: [:new]

below the route for users#new in the routes.rb file.

And, if you don't want to go the resources way then add a new route:

post 'users', to: 'users#create'

Solution 4:[4]

In your config/routes.rb file add:

resources :users, except: [:new]

Solution 5:[5]

In your class UserController, use:

route: get /signup, to: 'user#new'
.
.
resources :users

where ":users" is plural, because you will register multiple users.

Solution 6:[6]

This happens when we have a form and Rails doesn't know where to submit the form when we press the submit button. For this, we need to go to routes and make sure we have resources:users:

Rails.application.routes.draw do

#all the other resources and routes

resources :users

end

In one of the other answers, I saw

resources :users, except[:new]

Use this only if we have already introduced the new action in the routes:

resources :posts

get 'signup', to: 'users#new 

resources :users, except[:new]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jon Schneider
Solution 3 the Tin Man
Solution 4 the Tin Man
Solution 5 the Tin Man
Solution 6 the Tin Man