Mohammed
Mohammed

Reputation: 575

Mongoid/Rails 4 Documents not found error

Im trying to use the alternative of "foreign keys", embedded_in and embeds_many in rails 4. I'm sure there is a way round this and its making sense to me so far

My Models:

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

Now I'm able to create a nested route such as: http://localhost:3000/lines/:line_id/stations with:

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

My Stations Controller:

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

but when I visit the route I get:

message: Document(s) not found for class Line with id(s) :line_id. summary: When calling Line.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): :line_id ... (1 total) and the following ids were not found: :line_id. resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.

Upvotes: 1

Views: 1078

Answers (1)

Thomas R. Koll
Thomas R. Koll

Reputation: 3139

In your browser don't type http://localhost:3000/lines/:line_id/stations but http://localhost:3000/lines/1/stations !

If your routes.rb doesn't have the following you shoudl add it.

resources :lines do
  resources :stations
end

PS: Please indent with two specs, they are common practice amongst ruby programmers.

Upvotes: 1

Related Questions