使用mongoid / mongodb / rails访问引用的字段

时间:2011-01-29 11:26:43

标签: ruby-on-rails ruby ruby-on-rails-3 mongodb mongoid

如果我有模特......

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”错误。

3 个答案:

答案 0 :(得分: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" %>

答案 1 :(得分:0)

您的post.rb文件有references_one :topic,但在索引视图中,您正在执行for topic in @post.topics,这意味着帖子可能包含许多主题。要么您需要更改模型以说出references_many :topics,要么更改您的观点,以便每个帖子只有一个主题。

答案 2 :(得分: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>