Mongoid / Rails 4找不到文档错误

时间:2016-03-14 20:21:17

标签: ruby-on-rails mongodb mongoid nosql

我试图在rails 4中使用“外键”的替代方法,embedded_in和embeds_many。我确信有一种方法可以解决这个问题,到目前为止它对我有意义

我的模特:

class Line
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :stations
  field :line, type: String
  index({ starred: 1 })
end


class Station
  include Mongoid::Document
  include Mongoid::Timestamps
  has_many :routes
  embedded_in :line, inverse_of: :stations 
  field :name, type: String
end

现在我可以创建一个嵌套路由,例如: http://localhost:3000/lines/:line_id/stations 用:

Rails.application.routes.draw do
  resources :lines do
  resources :stations
end
  resources :routes
  root 'lines#index'
end

我的电台控制器:

class StationsController < ApplicationController
  before_action :load_line
  before_action :set_station, only: [:show, :edit, :update, :destroy]

  # GET /stations
  # GET /stations.json
def index
  @stations = @line.stations
end

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

# GET /stations/new
def new
  @station = @line.stations.build
end

# GET /stations/1/edit
def edit
end

# POST /stations
# POST /stations.json
def create
  @station = @line.stations.build(station_params)

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

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

# DELETE /stations/1
# DELETE /stations/1.json
def destroy
  @station.destroy
  respond_to do |format|
    format.html { redirect_to stations_url, notice: 'Station was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_station
  @station = @line.stations.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def station_params
  params.require(:station).permit(:name)
end

def load_line
  @line = Line.find(params[:line_id])
end
end

但是当我访问路线时,我得到了:

  

消息:找不到具有id(s)的类Line的文档:line_id。摘要:使用id或id数组调用Line.find时,每个参数必须与数据库中的文档匹配,否则将引发此错误。搜索的是id(s):: line_id ...(总共1个)并且未找到以下ID:line_id。解决方案:搜索数据库中的id或将Mongoid.raise_not_found_error配置选项设置为false,这将导致返回nil,而不是在搜索单个id时引发此错误,或者在搜索时仅返回匹配的文档数倍。

1 个答案:

答案 0 :(得分:1)

在您的浏览器中输入http://localhost:3000/lines/:line_id/stations但不要http://localhost:3000/lines/1/stations

如果您的routes.rb没有以下内容,请随意添加。

resources :lines do
  resources :stations
end

PS:请缩进两个规格,它们是红宝石程序员中常见的做法。