【问题标题】:What am i missing in the controller?我在控制器中缺少什么?
【发布时间】: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


【解决方案1】:

您有一个额外的逗号 (,)。删除这个额外的,。以下代码取自 OP 的comment

class User < ActiveRecord::Base 
  authenticates_with_sorcery! 
  validates_confirmation_of :password, message: "Both fields must match", if :password
                                                                   # see|
end

【讨论】:

  • 谢谢!现在我的 if @user.save 有一个错误,我会尝试修复它
  • 从您的帖子中删除代码。并把你在评论中给出的那个放在那里。 @用户3407533。否则你会得到更多的反对票。这将阻止您发布更多问题。
  • @user3407533 不接受这个答案的原因是什么?
猜你喜欢
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多