2014-04-16 1 views
0

Я пытаюсь выполнить руководство по началу работы с Rails, за исключением того, что я используя фонды вместо статей и TimeDeltas вместо комментариев. Теперь, когда я иду к виду, я получаю эту ошибкуПопытка создания вложенного класса 'show - получение ошибки' ActiveRecord :: StatementInvalid в StocksController # show Не удалось найти таблицу 'time_deltas' '

ActiveRecord :: StatementInvalid в StocksController # показать не удалось найти стол '' time_deltas

на линии: @time_delta = @ stock.time_deltas.build

Heres мой взгляд:

<h1> Stock </h1> 
<table> 
    <tr> 
     <th>Stock</th> 
     <th>Hashtag</th>   
    </tr> 
     <tr> 
      <td><%= @stock.name %></td> 
      <td><%= @stock.hashtag %></td> 
     </tr> 
</table> 

<h2>Deltas: </h2> 
    <table> 
    <tr> 
     <th>Stock</th> 
     <th>Hashtag</th>  
    </tr> 
    <% @stock.deltas.each do |delta| %> 
    <tr> 
     <td><%= @delta.start %></td> 
     <td><%= @delta.length %></td> 
    </tr> 
    <% end %> 
</table> 

<h2>Add a TimeDelta:</h2> 
<%= form_for([@stock, @stock.time_deltas.build]) do |f| %> 
    <p> 
    <%= f.label :start %><br> 
    <%= f.text_field :start %> 
    </p> 
    <p> 
    <%= f.label :length %><br> 
    <%= f.text_area :length %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

<%= link_to 'Back', stocks_path%> 
<%= link_to 'Edit', edit_stock_path(@stock)%> 

Heres мой акции контроллер:

class StocksController < ApplicationController 
    def new 
     @stock = Stock.new 
    end 

    def index 
     @stocks = Stock.all 
    end 

    def create 
     # XXX Add columns for delta and current standing when we get there 
     # they can intiate to nil 
     @stock = Stock.new(stock_params) 
     if @stock.save 
      redirect_to @stock 
     else 
      render 'new' 
     end 
    end 

    def show 
     @stock = find_stock 
     @time_delta = @stock.time_deltas.build 
    end 

    def edit 
     @stock = find_stock 
    end 

    def update 
     @stock = find_stock 


     if @stock.update(stock_params) 
      redirect_to @stock 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @stock = find_stock 
     @stock.destroy 

     redirect_to stocks_path 
    end 

    private 
     def stock_params 
      params.require(:stock).permit(:name, :hashtag) 
     end 

     def find_stock 
     return Stock.find(params[:id]) 
    end 
end 

Heres мой TimeDelta контроллер

class TimeDeltasController < ApplicationController 
    def new 
    @stock = Stock.find(params[:stock_id]) 
    @time_delta = @stock.time_deltas.build 
    # respond_with(@stock, @time_delta) 
    end 
    def create 
    @stock = Stock.find(time_delta_params) 
    @time_delta = @stock.time_deltas.create(params[:stock]) 
    redirect_to stock_path(@stock) 
    end 

    private 
    def time_delta_params 
     params.require(:time_delta).permit(:start, :length) 
    end 
end 

Я попытался повторно запущен мои миграции, а также и не-пользы, любые предложения?

EDIT: Обе модели Stock:

class Stock < ActiveRecord::Base 
    has_many :deltas 
    has_many :time_deltas 
    validates :hashtag, :name , presence: true, :uniqueness => true, length: { minimum: 2 } 
end 

Delta Time:

class TimeDelta < ActiveRecord::Base 
    belongs_to :stock 
end 

Схема базы данных:

# encoding: UTF-8 
# This file is auto-generated from the current state of the database. Instead 
# of editing this file, please use the migrations feature of Active Record to 
# incrementally modify your database, and then regenerate this schema definition. 
# 
# Note that this schema.rb definition is the authoritative source for your 
# database schema. If you need to create the application database on another 
# system, you should be using db:schema:load, not running all the migrations 
# from scratch. The latter is a flawed and unsustainable approach (the more migrations 
# you'll amass, the slower it'll run and the greater likelihood for issues). 
# 
# It's strongly recommended that you check this file into your version control system. 

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

    create_table "delta", force: true do |t| 
    t.datetime "start" 
    t.integer "length" 
    t.integer "stock_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "delta", ["stock_id"], name: "index_delta_on_stock_id" 

    create_table "queries", force: true do |t| 
    t.text  "tweet" 
    t.integer "tweetId" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "stocks", force: true do |t| 
    t.text  "name" 
    t.text  "hashtag" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "time_delta", force: true do |t| 
    t.datetime "start" 
    t.integer "length" 
    t.integer "stock_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "time_delta", ["stock_id"], name: "index_time_delta_on_stock_id" 

end 
+0

Показать модель пожалуйста. –

+0

@OscarValdezEsquea добавил оба! – NNNNNNNNNNDelicious

+0

У вас есть таблица под названием time_deltas? –

ответ

0

Когда вы создали таблицу с помощью миграции имя вашего " time_deltas "фактически была создана как" time_delta ". Сохранение имени базы данных в единственном виде может вызвать проблемы в будущем. Лучшее предложение - переименовать свою базу данных в «time_deltas», и все будет хорошо. Кроме того, я заметил, что есть другая таблица с уникальной формой имени (delta). Я предлагаю вам переименовать его в «дельта».

Счастливое кодирование!

+0

еще раз спасибо! – NNNNNNNNNNDelicious