【问题标题】:NameError, uninitialized constant when adding to tableNameError,添加到表时未初始化的常量
【发布时间】:2019-12-02 01:57:43
【问题描述】:

我收到此错误: ScoersController 中的名称错误#create 未初始化的常量 Score::UserTo 有谁知道可能导致此错误的原因?我什至不知道这可能指的是什么未初始化的conatant。

这是我的ScoresController

class ScoresController < ApplicationController
  before_action :set_score, only: [:show, :edit, :update, :destroy]

  # GET /scores
  # GET /scores.json
  def index
    @scores = Score.all
  end

  # GET /scores/1
  # GET /scores/1.json
  def show
  end

  # GET /scores/new
  def new
    @score = Score.new(project_id: params[:project_id])
    @usersFor = User.all
    @left = User.all
  end

  # GET /scores/1/edit
  def edit
  end

  # POST /scores
  # POST /scores.json
  def create
    @usersFor = User.all
    @left = User.all
    @score = Score.new(score_params)

    respond_to do |format|
      if @score.save
        format.html { redirect_to @score, notice: 'Score was successfully created.' }
        format.json { render :show, status: :created, location: @score }
      else
        format.html { render :new }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /scores/1
  # PATCH/PUT /scores/1.json
  def update
    respond_to do |format|
      if @score.update(score_params)
        format.html { redirect_to @score, notice: 'Score was successfully updated.' }
        format.json { render :show, status: :ok, location: @score }
      else
        format.html { render :edit }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /scores/1
  # DELETE /scores/1.json
  def destroy
    @score.destroy
    respond_to do |format|
      format.html { redirect_to scores_url, notice: 'Score was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_score
      @score = Score.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def score_params
      params.require(:score).permit(:project_id, :user_to_id, :user_for_id, :score)
    end
end

这是我的scores/new.html.erb 文件:

<% @project = Project.find_by id: params[:project_id] %>
<h1>New Score</h1>

<%= form_with(model: @score, local: true) do |form| %>
  <% if @score.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@score.errors.count, "error") %> prohibited this score from being saved:</h2>

      <ul>
        <% @score.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <% @projects = Project.where(id: params[:project_id]) %>
  <% @usersFor = User.where(id: current_user.id) %>
  <% @given = Score.where(project_id: @project.id, user_for_id: current_user.id) %>
  <% @evaluated = [] %>
  <% @given.each do |gave| %>
    <% @teammate = User.find_by id: gave.user_to_id %>
    <% @evaluated.push(@teammate) %>
  <% end %>
  <% @team %>
  <% @teams = Team.where(team_type_id: @project.team_type_id) %>
  <% @userTeams = TeamMembership.where(user_id: current_user.id) %>
  <% @userTeams.each do |t| %>
   <% @tempTeam = Team.find_by id: t.team_id%>
    <% if @tempTeam.team_type_id == @project.team_type_id %>
      <% @team = @tempTeam %>
      <% end %>
    <% end %>
  <% @group = TeamMembership.where(team_id: @team.id) %>
  <% @left = [] %>
  <% @group.each do |g| %>
    <% @m = User.find_by id: g.user_id %>
    <% @left.push(@m) %>
  <% end %>
  <% @left = @left - @evaluated %>

  <div class="field">
    <%= form.label :project_id %>
    <%= collection_select(:score, :project_id, @projects,:id,:project_name) %>
  </div>

  <div class="field">
    <%= form.select :user_for_id, options_for_select(@usersFor.collect{ |user| [user.first_name + " "+ user.last_name, user.id]}) %>
  </div>

  <div class="field">
    <%= form.select :user_to_id, options_for_select(@left.collect{ |user| [user.first_name + " "+ user.last_name, user.id]}) %>
  </div>

  <div class="field">
    <%= form.label :score %>
    <%= form.number_field :score, id: :score_score %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>

  <% end %>

如果有帮助,这是我的 score 模型:

class Score < ApplicationRecord
  validates :score, numericality: { less_than_or_equal_to: 10,
                                    only_integer: true}, presence:
                true
  belongs_to :project
  belongs_to :user_to
  belongs_to :user_for
end

谢谢! 编辑:新的score 模型

class Score < ApplicationRecord
  validates :score, numericality: { less_than_or_equal_to: 10,
                                    only_integer: true}, presence:
                true
  belongs_to :project
  has_one :user_to, class_name: User
  has_one :user_for, class_name: User
end

编辑:完整的错误信息:

ScoresController#create 中的 ActiveRecord::StatementInvalid SQLite3::SQLException: 没有这样的表: main.user_fors: INSERT INTO "scores" ("project_id", "user_to_id", "user_for_id", "score", "created_at", "updated_at") VALUES (?, ?, ? , ?, ?, ?) 错误在ScoresController 中的if @score.save 行引发

【问题讨论】:

  • 你的用户模型是什么?

标签: ruby-on-rails


【解决方案1】:

您的关联显示 user_to,除非您另有说明,否则 rails 将尝试查找名为 UserTo 的类。您可以将class_name: User 传递给关联以告诉它要使用哪个类。

【讨论】:

  • 这些是脚手架的结果。我应该摆脱这些关联吗?
  • 如果您正在使用它们,则不会。如果你真的有user_touser_for,那么你应该保留它们,你只需要告诉rails 它应该实际使用哪个类。默认是关联的名称
  • 好的,现在我收到一个错误 SQLException: no such table: main.user_fors:INSERT INTO "scores"("project_id", ...)。这与之前的修复有关吗?
  • 为UsersFor添加类名
  • 我也为用户添加了它,我很抱歉我应该提到这一点。我编辑了我的帖子以包含新模型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
相关资源
最近更新 更多