Search code examples
ruby-on-railsruby-on-rails-4simple-form

simple_form undefined method `model_name' for NilClass:Class


I'm learning Rails, and I would like to use simple_form. I'm creating a simple app (my first one), and I want to create a signup form. I used rails g scaffold User username:string password:string to create my model and controller. I'm using Foundation gem too, if that's matter, and I install simple_form with the right command for Foundation. I've been looking for answers for two hours, I tried many things, I have no idea of what's wrong with my code.

## app/views/home/index.html.erb ##
<%= simple_form_for @User do |f| %>
    <%= f.input :username %>
    <%= f.input :password %>
<% end %>

## app/models/user.rb ##
class User < ActiveRecord::Base
end


## app/controllers/users_controller.rb
class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:username, :password)
    end
end

I'm obviously doing something wrong, and I'm pretty sure I will feel like an idiot, but it's my first app :/ Thanks you. EDIT: I forgot to say, I tried (desperately) every vars (@user, @users, @User, @Users), any of them doesn't works :/


Solution

  • Get rid of the form from the index.html.erb view (and no, don't put it in show.html.erb either!).

    Place this form in the new.html.erb view:

    <%= simple_form_for @user do |f| %>
      <%= f.input :username %>
      <%= f.input :password %>
      <%= f.submit %>
    <% end %>
    

    Note that the form should reference @user not @User.