2011-01-29 93 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>