Rails NoMethodError (undefined method `User' for #<User:0x00000002586a68>):

aini · 2018年08月20日 · 最后由 aini 回复于 2018年08月21日 · 2468 次阅读

学习 rails 5 遇到报错

描述

在我没有添加 validates 之前项目可以正常运行,添加了 validates 之后报错如下。报错之前我运行了命令↓

rails db:rollback
rails destroy scaffold Microposts
rails generate scaffold Micropost content:text user_id:integer

运行这几条命令是因为第一次运行 scaffold 的时候 user_id: integer 中的冒号误打成了中文的了

报错信息

Started POST "/users" for 106.37.100.61 at 2018-08-20 07:19:33 +0000
Cannot render console from 106.37.100.61! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
   (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"UrrKnE8xR9phK4WEdyrgxgJSItgopSno2ZfyTHC1AIKG3WEs4iE17gDzp5xGzxXSrLG0cqOPP1cht7AvOSQMqQ==", "user"=>{"name"=>"", "email"=>""}, "commit"=>"Create User"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 25ms (ActiveRecord: 0.8ms)



NoMethodError (undefined method `User' for #<User:0x00000002586a68>):

app/controllers/users_controller.rb:31:in `block in create'
app/controllers/users_controller.rb:30:in `create'

model(user.rb)

class User < ApplicationRecord
  has_many :microposts
  validates name, presence: true 
  validates email, presence: true 
end

controller (users_controller.rb)

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :set_user_micropost, 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 :show, status: :created, location: @user }
      else
        format.html { render :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 { render :show, status: :ok, location: @user }
      else
        format.html { render :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, notice: 'User was successfully destroyed.' }
      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

    def set_user_micropost
      @microposts = @user.microposts
    end

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

application_controller

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "Hello World!"
  end
end

先把这个语法错误改了:

# wrong
class User < ApplicationRecord
  has_many :microposts
  validates name, presence: true 
  validates email, presence: true 
end

# right
class User < ApplicationRecord
  has_many :microposts
  validates :name, presence: true 
  validates :email, presence: true 
end
Rei 回复

😂 谢谢!!!

也試試看寫成這樣吧

class User < ApplicationRecord
  validates :name, :email, presence: true
end
hechian 回复

谢谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册