2016-03-14 173 views
1

我試圖在rails 4中使用「外鍵」,embedded_in和embeds_many的替代方法。我確信有一種方法可以解決這個問題,它對我來說目前爲止還有意義Mongoid/Rails 4找不到文檔錯誤

我的模型:

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 

但是當我參觀路線,我得到:

消息:文件(S)未找到班線有ID(S):line_id。摘要:使用id或id數組調用Line.find時,每個參數必須與數據庫中的文檔匹配,否則將引發此錯誤。搜索的是id(s)::line_id ...(總共1個),但沒有找到以下id::line_id。解決方案:搜索數據庫中的id或將Mongoid.raise_not_found_error配置選項設置爲false,這將導致返回nil而不是在搜索單個id時引發此錯誤,或在搜索時僅返回匹配的文檔倍數。

+0

請將您的routes.rb文件的相關部分貼出。 – toddmetheny

+0

完成後,我更新了該文章 – Mohammed

+0

*請確保您的文件正確縮進*。看看routes.rb,你仍然可以正確地掌握那幾個嗎?我需要兩眼,因爲我的頭腦首先告訴我你在那裏有一個錯誤。 –

回答

1

在您的瀏覽器中不打印http://localhost:3000/lines/:line_id/stations但是http://localhost:3000/lines/1/stations

如果你的routes.rb沒有以下你應該添加它。

resources :lines do 
    resources :stations 
end 

PS:請縮進兩個規格,他們是ruby程序員之間的慣例。

+0

完成,如果我想擺脫看起來像這個'56e60a6a0903268b487f6845'的ID? – Mohammed

+0

像這樣的寶石https://github.com/digitalplaywright/mongoid-slug 或者你用數字或字母保留你自己的唯一索引字段 –