theJava
theJava

Reputation: 15034

undefined method `model_name' for User:Class

Below is my form.

<%= form_for @user, {class: "form-horizontal", id: "signupform"} do |f| %>    
<% end %>

Below is my PersonController

class PersonController < ApplicationController
  def new
      @user = User.new
  end    
end

When i try to navigate to http://localhost:3000/person/new, i get the following error below.

undefined method `model_name' for User:Class

User Model

class User << CustomService 
  verbose true

  get :all, '/users/'
  post :login, '/signin'

end

Upvotes: 0

Views: 877

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

User class is not an ActiveRecord model in your code. Therefore, when you create User.new, it's just returning the naked ruby class (which doesn't work with Rails)

Check Rudy Sidinger's answer for how to do this correctly


Further, you may wish to look at this part of your class:

  get :all, '/users/'
  post :login, '/signin'

Why are you defining routes in your model? I've only ever defined routes in the config/routes.rb file like so:

#config/routes.rb
resources :users, only: :index, path_names: { index: "all" } do #-> routes to /all for users idnex
    collection do
        post "signin", action: :login 
    end
end

Upvotes: 1

Mohanraj
Mohanraj

Reputation: 4200

I think you don't want to specify @user, instead just use symbol(:user). Please try it.

Your code should be something like this,

<%= form_for :user, url: users_path, method: :post, {class: "form-horizontal", id: "signupform"} do |f| %>    
<% end %>

note: users_path => your post path.

Upvotes: 2

Rudy Seidinger
Rudy Seidinger

Reputation: 1058

Probably your User class is not inherinting from ActiveRecord::Base, like this:

class User < ActiveRecord::Base
 #your attributes and methods
end

Try to inherit User from this base class.

Hope it helps.

Upvotes: 1

Related Questions