NilClass:Class 的 simple_form 未定义方法 `model_name' [英] simple_form undefined method `model_name' for NilClass:Class

查看:58
本文介绍了NilClass:Class 的 simple_form 未定义方法 `model_name'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Rails,我想使用 simple_form.我正在创建一个简单的应用程序(我的第一个应用程序),并且我想创建一个注册表单.我使用 rails g scaffold User username:string password:string 创建我的模型和控制器.我也在使用 Foundation gem,如果这很重要,我会使用正确的 Foundation 命令安装 simple_form.我找了两个小时的答案,我尝试了很多东西,我不知道我的代码有什么问题.

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

我显然做错了什么,我很确定我会觉得自己像个白痴,但这是我的第一个应用程序:/谢谢.我忘了说,我(拼命地)尝试了每个变量(@user、@users、@User、@Users),它们中的任何一个都不起作用:/

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. I forgot to say, I tried (desperately) every vars (@user, @users, @User, @Users), any of them doesn't works :/

推荐答案

index.html.erb 视图中删除表单(不,不要把它放在 show.html.erb 要么!).

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

将此表单放在 new.html.erb 视图中:

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

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

注意表单应该引用@user而不是@User.

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

这篇关于NilClass:Class 的 simple_form 未定义方法 `model_name'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆