无法使用控制器

时间:2017-08-25 02:06:17

标签: mysql ruby-on-rails ruby model ruby-on-rails-5

我有两个型号 - CallDetail和Agent。代理表使用来自call_details表的外键。

架构粘贴在下面。

ActiveRecord::Schema.define(version: 20170824171227) do

  create_table "agents", force: :cascade do |t|
    t.integer "agent_id"
    t.string "name"
    t.integer "CallDetail_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["CallDetail_id"], name: "index_agents_on_CallDetail_id"
  end

  create_table "call_details", force: :cascade do |t|
    t.integer "call_id"
    t.string "word"
    t.float "start_time"
    t.float "end_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

在上面的数据库模式中,除了显示的属性外,rails默认创建CallDetail ID和Agent ID属性。

My Agents控制器创建功能如下所示

def create
    @agent = Agent.new(agent_params)

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

我的模型如下:

class CallDetail < ApplicationRecord
end
class Agent < ApplicationRecord
  belongs_to :CallDetail
end

当我打开localhost:3000 / agents并尝试在数据库中创建新条目时,会显示以下错误。

SQLite3 :: SQLException:没有这样的表:main.CallDetails:INSERT INTO“agents”(“agent_id”,“name”,“CallDetail_id”,“created_at”,“updated_at”)VALUES(?,?,?, ?,?)

enter image description here

我是铁杆新手。我在这里做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

Rails使用变量来更改模型的名称(以及其他内容)。您错误地引用了CallDetail。引用CallDetail时,应该使用snake_case,而不是CamelCase。您正在调用CallDetail,您应该在其中调用call_detail。

请注意,在您的架构文件中,CallDetail已转换为call_details。这是Rails使用变形的方法之一。您可以阅读有关变形here的信息。

相关问题