2011-01-29 23 views
2

如果我有一個模型......訪問使用mongoid /的MongoDB /導軌

class Post 
    include Mongoid::Document 
    field :link 
    field :title 
    field :synopsis 
    field :added_on, :type => Date 

    validates_presence_of :link 

    embeds_many :replies 
    references_one :topic 
end 

class Topic 
    include Mongoid::Document 
    field :category 

    referenced_in :post 
end 

什麼會我需要在index.html.erb的代碼訪問的基準場主題中的數據或添加要發佈的主題。

我試過post.topic但我得到一個未定義的方法錯誤。

非常感謝。

編輯:

下面是index.html的代碼

<div id="post"> 

    <% @posts.each do |post| %> 
     <div class="title_container"> 
      <%= link_to post.title, post.link %> || <%= link_to 'Edit', edit_post_path(post) %> || <%= post.topic %> 
     </div> 
    <% end %> 

    <br /> 


    <h2>Topics<h2> 
    <% for topic in @post.topics %> 
     <h3><%= topic.category %></h3> 
    <% end %> 


</div> 

這裏是posts_controller

class PostsController < ApplicationController 
    # GET /posts 
    # GET /posts.xml 
    def index 
    @posts = Post.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @posts } 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.xml 
    def show 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @post } 
    end 
    end 

    # GET /posts/new 
    # GET /posts/new.xml 
    def new 
    @post = Post.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @post } 
    end 
    end 

    # GET /posts/1/edit 
    def edit 
    @post = Post.find(params[:id]) 
    end 

    # POST /posts 
    # POST /posts.xml 
    def create 
    @post = Post.new(params[:post]) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to(@post, :notice => 'Post was successfully created.') } 
     format.xml { render :xml => @post, :status => :created, :location => @post } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /posts/1 
    # PUT /posts/1.xml 
    def update 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     if @post.update_attributes(params[:post]) 
     format.html { redirect_to(@post, :notice => 'Post was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.xml 
    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 

    respond_to do |format| 
     format.html { redirect_to(posts_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

編輯:

我也加入相關_form.html .erb代碼。

<div class="field"> 
    <%= f.label :topic_id %> 
    <%= f.collection_select :topic, Post.topic, :id, :category, :prompt => "Select a Topic" %> 
</div> 

編輯:

更新到2.0.0.rc.7仍不能得到它。

爲了好玩,嘗試了railscast視頻中的關鍵方法(http://railscasts.com/episodes/238-mongoid)。我在「PostsController#update」中得到「BSON :: InvalidObjectId」錯誤。

+0

@ user593120,我們可以更好地幫助你,如果你發佈index.html.erb位指示和方法的相關部分。 – hade 2011-01-29 11:33:13

+0

@ user593120你也可以發佈控制器代碼嗎? – Dogbert 2011-01-29 11:35:14

+0

@hade @Dogbert我已更新原始帖子。這是有用的信息嗎? – moctopus 2011-01-29 14:37:08

回答

1

我猜測的話題有很多的職位?如果您想要引用關聯,則需要將其更改爲此。

class Post 
    #... 
    referenced_in :topic 
end 

class Topic 
    #... 
    references_many :posts 
end 

然後試着改變你的collection_select行這樣的:

<%= f.collection_select :topic_id, Topic.all, :id, :category, :prompt => "Select a Topic" %> 
0

你的post.rb文件有一個references_one :topic,但在你的索引視圖中,你在做for topic in @post.topics,這意味着一個帖子可以有很多主題。無論是你需要改變你的模型來說references_many :topics或改變你的看法工作,每個職位只有一個主題。

+0

我改變了我的索引只使用<%= post.topic%>,我沒有得到任何返回值,這導致我相信我的代碼可能不保存選定的類別? – moctopus 2011-01-29 17:00:10

0

@ user593120 你有沒有在你的index.html.erb嘗試這種

<div id="post"> 

    <% @posts.each do |post| %> 
     <div class="title_container"> 
      <%= link_to post.title, post.link %> || <%= link_to 'Edit', edit_post_path(post) %> || <%= post.topic %> 
     </div> 
    <% end %> 

    <br /> 


    <h2>Topics<h2> 
    <% @posts.each do |post| %> 
     <h3><%= post.topic.category if post.topic %></h3> 
    <% end %> 


</div>