2012-03-12 57 views
0

enter image description hereRails的mongoid三模型关系

我已经在上面蓝色描绘了关系,但是,例如提示许多错误没有方法错误。

A和C之间的嵌入关系(红色)是OKay吗?鉴于另外两种关系。

此外,在路由文件中,我应该嵌套B在A?

请指教,谢谢。

class Trip 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :name 
    key :name 

    belongs_to :user 
    has_many :logs 
end 

class Log 
    include Mongoid::Document 
    field :content 
    validates_presence_of :content 
    belongs_to :trip 
    belongs_to :user 
end 

class User 
    include Mongoid::Document 
    has_many :trips 
    has_many :logs 
end 

错误:

NoMethodError in Trips#show 

Showing /home/jason/apps/app/views/trips/show.html.haml where line #17 raised: 

undefined method `logs_path' for #<#<Class:0x9b8a14c>:0x9d3251c> 

控制器:

class TripsController < ApplicationController 
    def show 
    @trip = Trip.find(params[:id]) 
    @log = Log.new 
    @logmsg = @trip.logs 
    end 
end 

class LogsController < ApplicationController 

    def create 
    @log = @trip.logs.build(params[:log]) 
    current_user.logs.build(params[:log]) 

    respond_to do |format| 
     if @log.save 
     format.html { redirect_to @log, notice: 'successfully created.' } 
     else 
     format.html { redirect_to trip_path(@trip) } 
     end 
    end 
    end 

    def destroy 
    I want an ajax destroy here, as the logs will only be shown in the Trips show page. 
    end 
end 

展会 - 车次/ show.html.haml:

= form_for @log do |f| 
    .field 
    = f.label :content 
    = f.text_field :content 
    .actions 
    = f.submit 'Save' 

下面是一个路由文件摘录:

App::Application.routes.draw do 
    resources :trips do 
    resources :logs 
    end 
end 
+0

你能说清楚'你应该在B中嵌套B'吗? – theTRON 2012-03-12 09:41:57

+0

我的意思是控制器设置。例如。 http:// localhost:3000/A(控制器)/ B(控制器)/ – 2012-03-12 14:42:41

回答

2

不,你不能嵌入在ModalCModalA,因为ModelB将无法​​引用嵌入的对象。

嵌入式模型实际上是里面的父文档,不在单独的集合中。

+0

但我参考了蓝色手写所描绘的模型,但仍然无法正常工作。请告诉我如何将三个他们?谢谢。 – 2012-03-12 14:47:14

+0

如果你可以更新你的问题,包括你的模型代码和你得到的确切的错误,它会更容易。 – theTRON 2012-03-12 21:20:46