simple_form undefined method `model_name' for NilClass:Class

最后都变了- 提交于 2020-01-16 02:28:10

问题


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 :/


回答1:


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.




回答2:


Yep ... in the view you are using the @User instance variable (with uppercase U) but in the controller you assign the model to the @user instance variable (with lowecase u) :)




回答3:


Put @user in lowcase (not @user)

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

And save it as new.html.erb view not index.html.erb!



来源:https://stackoverflow.com/questions/21841055/simple-form-undefined-method-model-name-for-nilclassclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!