【发布时间】:2023-03-11 04:21:01
【问题描述】:
这个 ruby 类给我一个错误 User is the name of the table,所以我不知道为什么它说索引方法有错误,因为假设 Users.all 必须返回 nil C:/ProyectosRails/gag_cf/app/models/user.rb:4: 语法错误,意外'\n',期待 tASSOC
提取的源代码(第 7 行附近):
# GET /users.json
def index
@users = User.all
end
这是控制器
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.all
end
def show
end
def new
@user = User.new
end
def edit
end
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
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
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates_confirmation_of :password, message: "Both fields must match", if :password
end
【问题讨论】:
-
问题似乎出在您的用户模型上,而不是您的控制器上,但如果您将控制器的完整源代码粘贴到那里,您将丢失并在销毁操作后“结束”
-
它说错误在模型中。型号代码在哪里
-
@rainkinz 不,我没有粘贴完整的源代码看这里是用户模型类 User
-
把它放在你的问题中,这样它就可以被格式化。错误消息非常明确地说明了错误的位置(或至少开始),只有包含它才有意义。
标签: ruby ruby-on-rails-4